Surc
Surc

Reputation: 45

How can I make an string form an array?

I create an array with output:

#items: array:3 [▼
    0 => array:4 [▼
      0 => "2019-11-23"
      1 => 5
      2 => "5"
      3 => "5"
    ]
    1 => array:4 [▼
      0 => "2019-11-24"
      1 => "12"
      2 => "0"
      3 => "0"
    ]

now i what to have in one row all values from [1] like: $value = [5, 12] i tried to do this with a foreach loop, i get the value's but i down know how to put them together like above desired example ( $value = [5, 12] )

Upvotes: 0

Views: 65

Answers (2)

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111839

Assuming you have data assigned to Laravel collection (and it seems you have them in collection) like this:

$data = collect([
    [
        "2019-11-23",
        5,
        "5",
        "5",
    ],
    [
        "2019-11-24",
        "12",
        "0",
        "0",
    ],
]);

When you use:

$result = $data->pluck('1')->all();

you will get:

array:2 [▼
  0 => 5
  1 => "12"
]

and if you need to cast all values to integers you can use:

$result = array_map('intval', $data->pluck('1')->all());

and the result will be

array:2 [▼
  0 => 5
  1 => 12
]

Upvotes: 1

Elias Soares
Elias Soares

Reputation: 10254

You can use array_map():

$originalArray = [
    ["2019-11-23", 5, "5", "5"],
    ["2019-11-24", "12", "0", "0"],
]

$outputArray = array_map(function($value) {
    return $value[1]; 
}, $originalArray);

Upvotes: 0

Related Questions