PDA

View Full Version : get specific value from array



UD2006
10-13-2011, 10:25 AM
Hi guru's,

I hope you can help me with the following:

I've made an array from a database, which contains a column stylesheet:
looks like this:



background-attachment:scroll; background-color:rgb(255, 255, 255); background-image:none; background-position:0% 0%; background-repeat:repeat; border-bottom-color:rgb(255, 0, 0); border-bottom-style:none; border-bottom-width:0px; border-collapse:separate; border-left-color:rgb(255, 0, 0);.........



where a complete style of a page is saved into.
The array works perfect ( it looks like this:



0=> background-attachment:scroll
1=> background-color:rgb(255, 255, 255)
2=> background-image:none
3=> background-position-x:0%
4=> background-position-y:0%
5=> background-repeat:repeat
6=> border-bottom-color:rgb(255, 0, 0)
7=> border-bottom-style:none
8=> border-bottom-width:0px
9=> border-collapse:separate
10=> border-left-color:rgb(255, 0, 0)
..........


What I want is to get a specific value (let's say, Height: 250px;) and only the 250px and use that as css for a canvas (html5).

So how can I search for that value and get it (or part of that value).
The code I have:



$array1 = explode(";", $pageStylesheet);
/* echo "<pre>";
print_r($array1);
echo "</pre>"; */
foreach ($array1 as $k => $v)
echo $k.'=>'.$v.'<br/>';


$find = "height:53px";
$keyFound = array_search($find, $array1);
if ($keyFound) {
echo "Item found: $keyFound";
}
else {
echo "Nothing found.";
}


Thanks

Fou-Lu
10-13-2011, 03:58 PM
Array search cannot be used. That's only useful if the value matches what is provided, and even then you certainly don't want to do it in a foreach.
Use strpos (http://php.ca/manual/en/function.strpos.php) instead.

UD2006
10-14-2011, 07:40 AM
Thanks for the reply,

I've looking around, try some things and came up with the following code:


$array1 = explode(";", $pageStylesheet);
foreach ($array1 as $k => $v)
echo $k.'=>'.$v.'<br/>'; /* display array */

$find = "height:";
foreach ($array1 as $phrase) {
if (strstr($phrase, $find)) $result = $phrase;
if(isset($result)) break;
}
echo $result.'<br/><br/>';

$short = $result;
$splittedstring=explode(":", $short);
echo "Short value of $result is: " .$splittedstring[1];

$db=null;

?>


This works pretty well, this is what I get back:


0=> #TXT-633-P1B1 { background-attachment:scroll
1=> background-color:rgb(255, 255, 255)
2=> background-image:none
3=> background-position:0% 0%
4=> background-repeat:repeat
5=> border-bottom-color:rgb(255, 0, 0)
6=> border-bottom-style:none
7=> border-bottom-width:0px
8=> border-collapse:separate
9=> border-left-color:rgb(255, 0, 0)
10=> border-left-style:none
11=> border-left-width:0px
12=> border-right-color:rgb(255, 0, 0)
13=> border-right-style:none
14=> border-right-width:0px
15=> border-top-color:rgb(255, 0, 0)
16=> border-top-style:none
17=> border-top-width:0px
18=> bottom:auto
19=> box-shadow:none
20=> caption-side:top
21=> color:rgb(255, 0, 0)
22=> font-family:GillSans
23=> font-size:24px
24=> font-style:normal
25=> font-variant:normal
26=> font-weight:bold
27=> height:53px
.........

height:53px

Short value of height:53px is: 53px


My only problem is, that when there are multiple items with the word "height" in it, only the first is displayed.
How can I get all the items, so I can reuse it in for example styling a div.

Fou-Lu
10-14-2011, 02:50 PM
Why not just split the array up into an associative array?


$aProps = array();
foreach ($array1 AS $prop)
{
list($key, $val) = explode(':', $prop);
$key = trim($key);
$val = trim($val);
if (!empty($key))
{
$aProps[$key] = $val;
}
}

Then you can simply access it under $aProps['height']. Then the only overwrite you'll get is whatever is overwritten in the css.

UD2006
10-17-2011, 10:36 AM
I've changed my code somewhat:


$query = $db -> prepare("select alpb_box_id, alpb_style::lvarchar as STYLESHEET from ao_link_page_boxes WHERE alpb_li_id = 633 AND alpb_lngcode = '031' AND alpb_pageno = 1");
$query -> execute();

$result = array();

while($row = $query->fetch(PDO::FETCH_ASSOC)) {

$result[] = $row;
}

$db = null;

echo "<pre>";
print_r($result);
echo "</pre>";


This gives me a result of:


Array
(
[0] => Array
(
[ALPB_BOX_ID] => TXT-633-P1B1
[STYLESHEET] => background-attachment:scroll; background-color:rgba(0, 0, 0, 0); background-image:none; background-position:0% 0%; background-repeat:repeat; border-bottom-color:rgb(0, 0, 0); border-bottom-style:none; border-bottom-width:0px; border-collapse:separate; border-left-color:rgb(0, 0, 0); border-left-style:none; border-left-width:0px; border-right-color:rgb(0, 0, 0); border-right-style:none; border-right-width:0px; border-top-color:rgb(0, 0, 0); border-top-style:none; border-top-width:0px; bottom:auto; box-shadow:none; caption-side:top; color:rgb(0, 0, 0); font-family:Swis721 BT; font-size:69px; font-style:normal; font-variant:normal; font-weight:normal; height:75px; left:0px; letter-spacing:normal; line-height:normal; list-style-image:none; list-style-position:outside; list-style-type:disc; margin-bottom:0px; margin-left:0px; margin-right:0px; margin-top:0px; max-height:none; max-width:none; min-height:0px; min-width:0px; overflow-x:hidden; overflow-y:hidden; padding-bottom:0px; padding-left:0px; padding-right:0px; padding-top:0px; position:relative; right:auto; text-align:-webkit-auto; text-decoration:none; text-indent:0px; text-overflow:clip; text-transform:none; top:0px; vertical-align:baseline; white-space:normal; width:793px; word-spacing:0px; z-index:auto;
)

[1] => Array
(
[ALPB_BOX_ID] => TXT-633-P1B2
[STYLESHEET] => background-attachment:scroll; background-color:rgba(0, 0, 0, 0); background-image:none; background-position:0% 0%; background-repeat:repeat; border-bottom-color:rgb(0, 0, 0); border-bottom-style:none; border-bottom-width:0px; border-collapse:separate; border-left-color:rgb(0, 0, 0); border-left-style:none; border-left-width:0px; border-right-color:rgb(0, 0, 0); border-right-style:none; border-right-width:0px; border-top-color:rgb(0, 0, 0); border-top-style:none; border-top-width:0px; bottom:auto; box-shadow:none; caption-side:top; color:rgb(0, 0, 0); font-family:\'Swis721 BT\'; font-size:16px; font-style:italic; font-variant:normal; font-weight:normal; height:197px; left:0px; letter-spacing:normal; line-height:normal; list-style-image:none; list-style-position:outside; list-style-type:disc; margin-bottom:0px; margin-left:0px; margin-right:0px; margin-top:0px; max-height:none; max-width:none; min-height:0px; min-width:0px; overflow-x:hidden; overflow-y:hidden; padding-bottom:0px; padding-left:0px; padding-right:0px; padding-top:0px; position:absolute; right:auto; text-align:-webkit-auto; text-decoration:none; text-indent:0px; text-overflow:clip; text-transform:none; top:75px; vertical-align:baseline; white-space:normal; width:717px; word-spacing:0px; z-index:auto;
)

[2] => Array
(
[ALPB_BOX_ID] => IMG-633-P1B3
[STYLESHEET] => background-attachment:scroll; background-color:rgba(0, 0, 0, 0); background-image:none; background-position:0% 0%; background-repeat:repeat; border-bottom-color:rgb(0, 0, 0); border-bottom-style:none; border-bottom-width:0px; border-collapse:separate; border-left-color:rgb(0, 0, 0); border-left-style:none; border-left-width:0px; border-right-color:rgb(0, 0, 0); border-right-style:none; border-right-width:0px; border-top-color:rgb(0, 0, 0); border-top-style:none; border-top-width:0px; bottom:auto; box-shadow:none; caption-side:top; color:rgb(0, 0, 0); font-family:\'Times New Roman\'; font-size:16px; font-style:normal; font-variant:normal; font-weight:normal; height:188px; left:0px; letter-spacing:normal; line-height:normal; list-style-image:none; list-style-position:outside; list-style-type:disc; margin-bottom:0px; margin-left:0px; margin-right:0px; margin-top:0px; max-height:none; max-width:none; min-height:0px; min-width:0px; overflow-x:visible; overflow-y:visible; padding-bottom:0px; padding-left:0px; padding-right:0px; padding-top:0px; position:relative; right:auto; text-align:-webkit-auto; text-decoration:none; text-indent:0px; text-overflow:clip; text-transform:none; top:207px; vertical-align:baseline; white-space:normal; width:377px; word-spacing:0px; z-index:auto;
)

[3] => Array
(
[ALPB_BOX_ID] => IMG-633-P1B4
[STYLESHEET] => background-attachment:scroll; background-color:rgba(0, 0, 0, 0); background-image:none; background-position:0% 0%; background-repeat:repeat; border-bottom-color:rgb(0, 0, 0); border-bottom-style:none; border-bottom-width:0px; border-collapse:separate; border-left-color:rgb(0, 0, 0); border-left-style:none; border-left-width:0px; border-right-color:rgb(0, 0, 0); border-right-style:none; border-right-width:0px; border-top-color:rgb(0, 0, 0); border-top-style:none; border-top-width:0px; bottom:auto; box-shadow:none; caption-side:top; color:rgb(0, 0, 0); font-family:Times New Roman; font-size:16px; font-style:normal; font-variant:normal; font-weight:normal; height:188px; left:0px; letter-spacing:normal; line-height:normal; list-style-image:none; list-style-position:outside; list-style-type:disc; margin-bottom:0px; margin-left:0px; margin-right:0px; margin-top:0px; max-height:none; max-width:none; min-height:0px; min-width:0px; overflow-x:visible; overflow-y:visible; padding-bottom:0px; padding-left:0px; padding-right:0px; padding-top:0px; position:relative; right:auto; text-align:-webkit-auto; text-decoration:none; text-indent:0px; text-overflow:clip; text-transform:none; top:0px; vertical-align:baseline; white-space:normal; width:377px; word-spacing:0px; z-index:auto;
)

)


Now what I want is to get the following values from each ID (I need them separate), the height, width, top, left.

For example:

TXT-633-P1B1
height:75px;
left:0px;
top:0px;
width:793px;

And reuse them in a style of letīs say multiple divīs.

Thanks

fail
10-17-2011, 12:42 PM
Can't you do just.....:





foreach($array1 as $value) {

if(stristr($value, 'height:') {

$height = str_replace('height:','' ,$value);

}

}

// result: $height = 53px

UD2006
10-17-2011, 01:21 PM
Thanks for reply,

I get an error:

Parse error: syntax error, unexpected T_VARIABLE in


It's about this line:



$height = str_replace('height:','' ,$value);

fail
10-17-2011, 02:45 PM
Whoops, my error!



foreach($array1 as $value) {

if(stristr($value, 'height:')) { // I forgot one ')'

$height = str_replace('height:','' ,$value);

}

}

// result: $height = 53px

UD2006
10-17-2011, 03:06 PM
Whoops, my error!



foreach($array1 as $value) {

if(stristr($value, 'height:')) { // I forgot one ')'

$height = str_replace('height:','' ,$value);

}

}

// result: $height = 53px



Thanks for your reply, but it isn't really what I am looking for, because there are other words (styles) in the array that has the name height in it.

If you look at my reply (just before you replied), I hope to get the exact items (locked to the special ID) and store them as result so I can reuse/echo them when needed.

Thanks again

_Aerospace_Eng_
10-17-2011, 03:09 PM
IMO I think Fou-Lu's code will work best. I think you are making this harder than it has to be. If you don't understand something then just ask.

UD2006
10-17-2011, 03:42 PM
IMO I think Fou-Lu's code will work best. I think you are making this harder than it has to be. If you don't understand something then just ask.

I've tested the code Fou-Lu posted and what I got back as result is a different part of the array (because there is a word: min-height: 2px;) in it, the code removes the height part, I can fix that.
It also displays only 1 result, where there should be multiple.

But my question is how to get from (for example) the ID: TXT-633-P1B1 the following back (check the array)

height:75px;
left:0px;
top:0px;
width:793px;

If I don't understand it, I will ask. But from what I saw is that the code only displayed 1 result instead (in this case) 4 results.

Thanks again, and to Fou-Lu also thanks.

_Aerospace_Eng_
10-18-2011, 04:44 AM
His code does not remove the height. The key to 2px; will be aProps['min-height'], it only explodes at the : part, not the -. What is your current code?

UD2006
10-18-2011, 07:42 AM
His code does not remove the height. The key to 2px; will be aProps['min-height'], it only explodes at the : part, not the -. What is your current code?

Here is my current code:



$array1 = array();
$pageStylesheet = " ";
$dataset = $db -> query("select alpb_box_id, alpb_style::lvarchar as STYLESHEET from ao_link_page_boxes WHERE alpb_li_id = 633 AND alpb_lngcode = '031' AND alpb_pageno = 1");
while($row = $dataset -> fetch()) {
$pageStylesheet = $pageStylesheet.trim("#".$row['ALPB_BOX_ID']."\n {\n".stripslashes ($row['STYLESHEET'])."\n");
}

$array1 = explode(";", $pageStylesheet);
foreach($array1 as $value) {
if(stristr($value, 'height:')) {
$height = str_replace('height:','' ,$value);
}
}
echo $height;


The ouput is:

min-0px

This is also strange because the height is specified before min-height.

Thanks

UD2006
10-18-2011, 12:56 PM
I have found an other code, which does what it should:


$query = $db -> prepare("select alpb_box_id, alpb_style::lvarchar as STYLESHEET from ao_link_page_boxes WHERE alpb_li_id = 633 AND alpb_lngcode = '031' AND alpb_pageno = 1");
$query -> execute();

$result = array();

while($row = $query->fetch(PDO::FETCH_ASSOC)) {

$result[] = $row;
}

$new_styles = array();
$styles_to_collect = array('height', 'width', 'top', 'left');
foreach($result as $r)
{
$new_entry = array();
foreach($styles_to_collect as $stc)
{
preg_match('/[\s]\s*'.$stc.':\s*([\d]+(px)?)([;\s]|$)/', $r['STYLESHEET'], $matches);
$new_entry[$stc] = $matches[0];
}
$new_styles[$r['ALPB_BOX_ID']] = $new_entry;
}
$db = null;

echo "<pre>";
print_r($new_styles);
echo "</pre>";


This gives me an output of:


Array
(
[TXT-633-P1B1] => Array
(
[height] => height:75px;
[width] => width:793px;
[top] => top:0px;
[left] => left:0px;
)

[TXT-633-P1B2] => Array
(
[height] => height:197px;
[width] => width:717px;
[top] => top:75px;
[left] => left:0px;
)

[IMG-633-P1B3] => Array
(
[height] => height:188px;
[width] => width:377px;
[top] => top:207px;
[left] => left:0px;
)

[IMG-633-P1B4] => Array
(
[height] => height:188px;
[width] => width:377px;
[top] => top:0px;
[left] => left:0px;
)
)


The only thing I am looking for is to make div's from these items.
I already tried it, with the following code:


foreach($new_styles[$r['ALPB_BOX_ID']] as $divid) {
echo '<div id=".$divid." ';
foreach($new_entry as $v) {
echo 'style=".$v" >';
}
echo "</div>";
}


I've added this code between the line:

$new_styles[$r['ALPB_BOX_ID']] = $new_entry;
and the last }

But the output is:



<div id=".$divid." style=".$v" >style=".$v" >style=".$v" >style=".$v" ></div><div id=".$divid." style=".$v" >style=".$v" >style=".$v" >style=".$v" ></div><div id=".$divid." style=".$v" >style=".$v" >style=".$v" >style=".$v" ></div><div id=".$divid." style=".$v" >style=".$v" >style=".$v" >style=".$v" ></div><div id=".$divid." style=".$v" >style=".$v" >style=".$v" >style=".$v" ></div><div id=".$divid." style=".$v" >style=".$v" >style=".$v" >style=".$v" ></div><div id=".$divid." style=".$v" >style=".$v" >style=".$v" >style=".$v" ></div><div id=".$divid." style=".$v" >style=".$v" >style=".$v" >style=".$v" ></div><div id=".$divid." style=".$v" >style=".$v" >style=".$v" >style=".$v" ></div><div id=".$divid." style=".$v" >style=".$v" >style=".$v" >style=".$v" ></div><div id=".$divid." style=".$v" >style=".$v" >style=".$v" >style=".$v" ></div><div id=".$divid." style=".$v" >style=".$v" >style=".$v" >style=".$v" ></div><div id=".$divid." style=".$v" >style=".$v" >style=".$v" >style=".$v" ></div><div id=".$divid." style=".$v" >style=".$v" >style=".$v" >style=".$v" ></div><div id=".$divid." style=".$v" >style=".$v" >style=".$v" >style=".$v" ></div><div id=".$divid." style=".$v" >style=".$v" >style=".$v" >style=".$v" ></div>


Too many and not kinda what I expected.

I was looking for:

<div id="TXT-633-P1B1" style="height: 75px; width:793px; top:0px; left:0px;"></div>

The amount of div's depending on the array.

Thanks

_Aerospace_Eng_
10-19-2011, 04:04 AM
You aren't using your quotes correctly. I think you want something more like this

foreach($new_styles as $divid=>$styles) { // foreach on the array, pulling the key=>value pairs
echo '<div id="'.$divid.'" style="';
foreach($styles as $style) { // the value will be another array so foreach on that pulling just the values
echo $style.';';
}
echo '"></div>'."\n";
}