Phuong Ng
Phuong Ng

Reputation: 59

How to get each respective value of an array key in php

I have an array after doing theprint_r($arr); like this:

Array
(
   [0] => (orange, lemon, mango)
   [1] => (house, apartment, hostel)
   [2] => (one, two, three)
   [3] => (monday, tuesday, wednesday)
)

When I tried this code

foreach ($arr as $k => $v){
echo $k .'=>'. $v;

}

I got the result like this:

0=>(orange, lemon, mango)
1=> (house, apartment, hostel)
2 => (one, two, three)
3=> (monday, tuesday, wednesday)

However, what I would like to get is the the first value of each key, for example, orange, house, one, monday to put into a variable called $var1, and the second to put into an another variable called $var2, and the third to put into an another variable called $var3 respectively so that I can insert them into the MySQL table.

Your help is appreciated.

Thanks,

Upvotes: 0

Views: 71

Answers (3)

bestprogrammerintheworld
bestprogrammerintheworld

Reputation: 5520

I'm not sure if I got this right, but I think you wanted something like this.

$narr = [];
//Create a copy of the array $arr and remove parenthises and spaces in each item
foreach($arr as $item) {
    $item = str_replace("(","",$item);
    $item = str_replace(")","",$item);
    $item = str_replace(" ","",$item);
    $narr[] = explode(',',$item);
}

//Here we create a new array again based on array_column (0,1,2).
$new_arr = [];
for($i=0;$i<count($narr);$i++) {
    foreach(array_column($narr,$i) as $item) {
        $new_arr[$i][] = $item;
    }
}

The result of $new_arr would be:

Array
(
    [0] => Array
        (
            [0] => orange
            [1] => house
            [2] => one
            [3] => monday
        )

    [1] => Array
        (
            [0] => lemon
            [1] => apartment
            [2] => two
            [3] => tuesday
        )

    [2] => Array
        (
            [0] => mango
            [1] => hostel
            [2] => three
            [3] => wednesday
        )

)

I know you said in your question that you wanted $var1, $var2 but this is what arrays are for and therefore you get the result in the array.


array_column() works like this:

You have the array $array:

[0] => Array
    (
        [0] => orange
        [1] => house
        [2] => one
        [3] => monday
    )


[1] => Array
    (
        [0] => lemon
        [1] => apartment
        [2] => two
        [3] => tuesday
    )

[2] => Array
    (
        [0] => mango
        [1] => hostel
        [2] => three
        [3] => wednesday
    )


array_column($array,0) contains: orange, lemon, mango
array_column($array,1) contains: house, apartment, hostel
etc...

Upvotes: 1

JureW
JureW

Reputation: 683

Base on your request, you can do it like this:

<?php
$array = ["(orange, lemon, mango)","(house, apartment, hostel)","(one, two, three)","(monday, tuesday, wednesday)"];
$modifiedArray = [];
foreach($array as $single) {
    $single = str_replace(array( '(', ')' ), '', $single);
    $singleArray = explode(",",$single);
    foreach($singleArray as $key => $v) {
        $modifiedArray[$key][] = $v;
    }
}
foreach($modifiedArray as $key => $one) {
    $modifiedArray[$key] = implode(",",$one);
}
print("<pre>".print_r($modifiedArray,true)."</pre>");
?>

Which will print out:

Array
(
    [0] => orange,house,one,monday
    [1] =>  lemon, apartment, two, tuesday
    [2] =>  mango, hostel, three, wednesday
)

Upvotes: 0

Professor Abronsius
Professor Abronsius

Reputation: 33813

Assuming that the values in the array are strings ( which is suggested by the use of echo $k.'='.$v;) then perhaps this might help

$arr=[
    '(orange, lemon, mango)',
    '(house, apartment, hostel)',
    '(one, two, three)',
    '(monday, tuesday, wednesday)'
];

foreach( $arr as $str ){
    preg_match('@\w+@',$str,$match);
    echo $match[0] . '<br />';
}

Which outputs:

orange
house
one
monday

Upvotes: 0

Related Questions