Jump to content

Extracting only the Last Array's Child elemets


natasha_thomas

Recommended Posts

Folks,

 

I have an Array as shown in the below Structre (An array has multiple parents and childs)

 

Array $productlist

 

 

Array

{

1 => A

2 => B

3 => C

 

}

 

 

 

Array

{

1 => A

2 => B

3 => C

4 => D

5 => E

}

 

 

 

Array

{

1 => A

2 => B

3 => C

4 => D

5 => E

6 => E

7 => E

8 => E

}

 

 

 

$productlist Array can have N numbers of Parents array as shown in above example, (above its 3 it can be anything).

 

All i want is, i want to print_r only the last Parent rray and all its child elements. So in this case it will be:

 

 

Array

{

1 => A

2 => B

3 => C

4 => D

5 => E

6 => E

7 => E

8 => E

}

 

 

 

HOw can i achieve this with PHP, i tried even count() but its not working not even array_pop().

 

Can someone help me with this?

 

Regards,

Natasha

Link to comment
Share on other sites

Well, you could just do this:

 

$array = array(array("1" => "A"), array("2" => "B", "3" => "C"), array("4" => "D"));

foreach($array as $child) $temparray = $child;

print_r($temparray); // Should contain the last element in $array.

// Output: array { "4" => "D" }

Link to comment
Share on other sites

Well, you could just do this:

 

$array = array(array("1" => "A"), array("2" => "B", "3" => "C"), array("4" => "D"));

foreach($array as $child) $temparray = $child;

print_r($temparray); // Should contain the last element in $array.

// Output: array { "4" => "D" }

 

 

I did this, but the output is the Last child node of each Parent Array, Its not what i am looking for.

 

All i want is, the code should give me All the Childs of the last parent Node. haev a look at my Example above, so the desired output will be:

 

 

 

Array

{

1 => A

2 => B

3 => C

4 => D

5 => E

6 => E

7 => E

8 => E

}

 

Make Sense?

 

Cheers

NT

 

Link to comment
Share on other sites

print_r( end($productlist) );

 

Just goes to show that there's always a more efficient way to do things in PHP :P

 

I did this, but the output is the Last child node of each Parent Array, Its not what i am looking for.

 

Take a look ad AbraCadaver's code above. That should work as you want it to, although so should mine...

 

Link to comment
Share on other sites

print_r( end($productlist) );

 

Just goes to show that there's always a more efficient way to do things in PHP :P

 

I did this, but the output is the Last child node of each Parent Array, Its not what i am looking for.

 

Take a look ad AbraCadaver's code above. That should work as you want it to, although so should mine...

 

Many thanks, the one worked.

 

So the output is all CHild nodes of LAst parent.

 

Another complication is: Echo Child Nodes of Last parent node has multiple words in it. So consider $lastArry as the last parent node and here are its Child:

 

child 1 : Paintball mask

child 2 : paintball helmet

child 3 : sniper ball

 

What i want is to make a new array where each word of Each Child node in Last Parent node becomes a Child of New Array Like:

 

NewArray

{

1 = > paintball

2 => mask

3 => paintball

4 => helmet

5 =>sniper

6 => ball

}

 

 

How can i achieve this?

 

I have used Explode() to do this, but could not achieve this.

 

Cheers

Natasha

Link to comment
Share on other sites

$array = array("Paintball Mask", "Paintball Helmet", "Sniper Ball");

$newarray = array();
foreach($array as $child) {
    $temp = explode(" ", $child);
    foreach($temp as $tchild) {
        $newarray[] = $tchild;
    }
}

print_r($newarray);

Link to comment
Share on other sites

$array = array("Paintball Mask", "Paintball Helmet", "Sniper Ball");

$newarray = array();
foreach($array as $child) {
    $temp = explode(" ", $child);
    foreach($temp as $tchild) {
        $newarray[] = $tchild;
    }
}

print_r($newarray);

 

 

Thanks Mattle, but this code is not working.

 

Once again, below is the String i got after imploding all the Children of last Array:

 

airganix na50 air purifier nscessity: hepa air purifier heaven fresh hf 200 ionic air purifier [baby product] vicks v-9071 hepa air purifier heaven fresh hf 210uv ioninc air purifier w/fan & uv lamp [baby product] heaven fresh hf 100 air purifier [baby product] lloydspharmacy ionising air purifier nscessity: tio2 uv plasma air purifier cleanaer air purifier - removes allergens, dust & smoke from the air you breathe pure allergy cartridge & batteries for cleanaer air purifier

 

Now all i want is, to have Each word of this String in an Array....  I tried to use explode(), but i could not make that work..

 

Did i miss something?

 

Cheers

NT

Link to comment
Share on other sites

If you could post the code you currently have (perhaps only the relevant parts), it should hopefully stop the second guessing that's going on.

 

Mike.

 

 $prodname[] = strtolower($product->name);
  $prodlast = end($prodname); 
echo $prodlast;

 

Gave me

 

airganix na50 air purifier nscessity: hepa air purifier heaven fresh hf 200 ionic air purifier [baby product] vicks v-9071 hepa air purifier heaven fresh hf 210uv ioninc air purifier w/fan & uv lamp [baby product] heaven fresh hf 100 air purifier [baby product] lloydspharmacy ionising air purifier nscessity: tio2 uv plasma air purifier cleanaer air purifier - removes allergens, dust & smoke from the air you breathe pure allergy cartridge & batteries for cleanaer air purifier

 

But funny thing is, this output itself is an array like:

 

After doing

echo '<pre>'; 

            echo $prodlast;

            echo '</pre>';

           

 

Output was:

bliss aroma diffuser aqua air purifier humidifier air freshener essential oil diffuser scent...

nirvana aroma diffuser air purifier humidifier air freshener essential oil diffuser scent diffuser

bliss aroma diffuser amber air purifier humidifier air freshener essential oil diffuser scent...

enso aroma diffuser air purifier humidifier air freshener essential oil diffuser scent diffuser blue

premiair hepa filter air purifierfresh air globe purifier ioniser & humidifier colour changing led ligh

new! ideal birthday gift set!! enso aroma diffuser air purifier humidifier air freshener essential...

sharp kc860ekw air purifier with humidifier - whiteaeromag non-electric evaporative air humidifier & purifier (50 cm, cca. 0.6 litre

 

 

All i want is, each word to be the child of a New Array.

 

Natasha

Link to comment
Share on other sites

$result = array();

foreach($prodlast as $child) {
    $result = array_merge($result, explode(' ', $child));
}
print_r($result);

 

 

 

 

I edited my code as you suggested:

  $prodlast = end($prodname);

           

           

           

            $result = array();

            foreach($prodlast as $child)

            {   

                $result = array_merge($result, explode(' ', $child));

                }

                echo '<pre>';

                print_r($result);

                echo '</pre>';

           

 

Output:

Array

(

)

Array

(

)

Array

(

)

Array

(

)

Array

(

)

Array

(

)

Array

(

)

Array

(

)

Array

(

)

Array

(

)

Link to comment
Share on other sites

Give my code a whack:

 

$prodname[] = strtolower($product->name);
$prodlast = end($prodname);

$prodtags = array();
foreach($prodlast as $child) {
    $temp = explode(" ", $child);
    foreach($temp as $tchild) {
        $prodtags[] = $tchild;
    }
}

print_r($prodtags);

Link to comment
Share on other sites

Well, I am confused about all the variations of your data and how it seems to change.  This works:

 

$prodlast[] = 'Paintball mask';
$prodlast[] = 'paintball helmet';
$prodlast[] = 'sniper ball';

$result = array();

foreach($prodlast as $child) {
    $result = array_merge($result, explode(' ', $child));
}
print_r($result);

 

It yields:

 

Array
(
    [0] => Paintball
    [1] => mask
    [2] => paintball
    [3] => helmet
    [4] => sniper
    [5] => ball
)

Link to comment
Share on other sites

Well, I am confused about all the variations of your data and how it seems to change.  This works:

 

$prodlast[] = 'Paintball mask';
$prodlast[] = 'paintball helmet';
$prodlast[] = 'sniper ball';

$result = array();

foreach($prodlast as $child) {
    $result = array_merge($result, explode(' ', $child));
}
print_r($result);

 

It yields:

 

Array
(
    [0] => Paintball
    [1] => mask
    [2] => paintball
    [3] => helmet
    [4] => sniper
    [5] => ball
)

 

Just use this. Much better than my solution.

Link to comment
Share on other sites

Give my code a whack:

 

$prodname[] = strtolower($product->name);
$prodlast = end($prodname);

$prodtags = array();
foreach($prodlast as $child) {
    $temp = explode(" ", $child);
    foreach($temp as $tchild) {
        $prodtags[] = $tchild;
    }
}

print_r($prodtags);

 

 

output is:

 

Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( )

 

:'(

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.