4

I have the below php array $tempStyleArray which is created by splitting a string.

$tempStyleArray = preg_split( "/[:;]+/", "width: 569px; height: 26.456692913px; margin: 0px; border: 2px solid black;" );

Produces:

array (
  0 => 'width',
  1 => ' 569px',
  2 => ' height',
  3 => ' 26.456692913px',
  4 => ' margin',
  5 => ' 0px',
  6 => ' border',
  7 => ' 2px solid black',
  8 => '',
)

I have to get index/key of the element height from this array. I tried below codes but nothing seems to be working for me.

foreach($tempStyleArray as  $value)
{
   if($value == "height") // not satisfying this condition
   {
     echo $value;
     echo '</br>';
     $key = $i;
   }
} 

in above solution it not satisfying the condition ever :(

$key = array_search('height', $tempStyleArray); // this one not returning anything

Help me to solve this? Is there any problem with my array format?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
chriz
  • 1,339
  • 2
  • 16
  • 32

6 Answers6

4
foreach($tempStyleArray as  $value)
{
   if($value == "height") // not satisfying this condition
   {
     echo $value;
     echo '</br>';
     $key = $i;
   }
} 

and what is $i?, better use key=>value for.

foreach($tempStyleArray as  $key => $value)
{
   if($value == "height") // not satisfying this condition
   {
     echo $value;
     echo '</br>';
     echo $key;
   }
} 

Looking at your array, it appears that you want to say that "width" will be 569px, then maybe is better to do this:

$tempStyleArray = array(
    "width" =>  "569px",
    "height" => "26.456692913px",
    "margin" => "0px",
    "border" => "2px solid black"
);

That way you can just say

echo $tempStyleArray["width"];

This will be faster and you don't have to due with searching.

UPDATE:

for( $i == 1; $i < count($tempStyleArray); $i = $i+2)
{
    $newArray[ $tempStyleArray[$i-1] ] = $tempStyleArray[$i]
}

with that you can get an hash based array.

lcjury
  • 1,158
  • 1
  • 14
  • 26
  • my bad, this solution not working.. but your suggestion is great. check my edited question.. i am generating this array by splitting a string, how i can make my array like u advised? – chriz Apr 22 '15 at 05:58
  • 1
    updated, if your "array" gonna be big and you need to query a lot to it, this would work better than using array_search. – lcjury Apr 22 '15 at 06:09
2

You are taking wrong values from array

It should be

 foreach($tempStyleArray as $temp => $value) {
    if($value == "height") // will satisfy this condition
    { 
        echo $value; 
        echo '</br>'; 
        $key = $i;
    }
 } 
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
2

Try this -

$tempStyleArray = array_map('trim', preg_split( "/[:;]+/", "width: 569px; height: 26.456692913px; margin: 0px; border: 2px solid black;" ));
var_dump($tempStyleArray);
$key = array_search('height', $tempStyleArray);
echo $key;

It was happening because there was space with the values in array. So needed to be trimmed. After splitting the string every value will be passed through the trim() so that the white spaces are removed.

Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
2

Use as follows(Treat like an associative array)

foreach($tempStyleArray as   $key => $value){
   if($value == "height") // not satisfying this condition
   {
     echo $value;
     echo '</br>';
     echo $key;
   }
}
1

There are errors in your code. Please try:

foreach($tempStyleArray as  $key => $value)
{
   if($value == "height") 
   {
     echo $key;
   }
} 
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
Nisha
  • 685
  • 1
  • 4
  • 16
0

The fault in your coding attempt was not in array_search(), but in the regex that was used to split the string into pieces.

preg_split() is perfectly capable of cleanly splitting on delimiting colons and semi-colons including any unwanted leading/trailing spaces. It is not necessary to iterate the array and trim every value.

Code: (Demo)

$tempStyleArray = preg_split(
    "/ *[:;]+ */",
    "width: 569px; height: 26.456692913px; margin: 0px; border: 2px solid black;",
    0,
    PREG_SPLIT_NO_EMPTY
);

var_export($tempStyleArray);

echo array_search('height', $tempStyleArray);

Then again if height was the first attribute and the style attribute declaration started with a space, the you would need to trim the style string before splitting it.


I think it is fair to presume, that the only reason to get the index of an attribute name would be to increment the index then access its value which would be in the next element. If this is true, then there are more direct or more reliable techniques.

Using regex, you could use preg_match('/height: *\K[^;]+/, $style, $match) then access the full string match (element [0] of $match) if it is populated.


All that said, if you are trying to parse valid HTML, then regex is not the most professional, reliable tool.

DOM parsers are designed to more reliably parse valid HTML -- often using a more intuitive syntax than regular expressions.

Valuable reading:

mickmackusa
  • 43,625
  • 12
  • 83
  • 136