Convert an Associative Array to a Simple Array of Its Values in PHP

Turn Associative Array into simple array

You can use implode function to convert the array into comma separated string. Since you want to convert each inner array into a string while keeping the array structure as it is, I have used a loop. You can use another way to reach to each inner array.

You can read more about implodeHERE

Try below

<?php

$final_array = [];

foreach($initial_array as $new_array){
$final_array[] = implode(',', $new_array)
}

Convert an associative array to a simple array of its values in php

simply use array_values function:

$array = array_values($array);

How to convert an associative array into a simple array in php?

For which purpose do you need the array?

If you want to use JSON data, just try to call json_encode on the array:

$array = array('NBA', 'MGNREGS');
var_dump($array);
print json_encode($array);

Output:

array(2) { 
[0]=> string(3) "NBA"
[1]=> string(7) "MGNREGS"
}

["NBA","MGNREGS"]

Convert associative array to indexed array with associative subarrays

Simply loop through it and create a new array from each key/value pair.

<?php
$array = array("country1" => "CountryOne", "country2" => "CountryTwo");

$newArray = array();

foreach($array as $key => $value) {
array_push($newArray, array("code" => $key, "name" => $value));
}

var_dump($newArray);
?>

how to convert associative array in to one array

You can use Laravel helper function array_flatten for this:

$array = [
0 => [
0 => 'Women',
],
1 => [
0 => 'children',
1 => 'smile',
],
2 => [
0 => 'Abstract',
],
3 => [
0 => 'Lion',
1 => 'Cheetah',
],
];

$result = array_flatten($array);

var_dump($result);

Output:

array (size=6)
0 => string 'Women' (length=5)
1 => string 'children' (length=8)
2 => string 'smile' (length=5)
3 => string 'Abstract' (length=8)
4 => string 'Lion' (length=4)
5 => string 'Cheetah' (length=7)

PHP convert simple array to associative array

Try this :)

$array2 = [];
foreach($array1 as $key => $val){
$array2[] = (object)["id" => $key, "count" => $val];
}

Convert every two values of an associative array into key-value pairs

Here is one way combining three PHP array functions:

$result = array_combine(...array_map(null, ...array_chunk($array, 2)));

array_chunk gives you

[
['orange', '100'],
['banana', '200'],
['apple', '300']
];

array_map converts that to:

[
['orange', 'banana', 'apple'],
['100', '200', '300'],
];

Which can feed directly into array_column.

convert std object array to simple php array

You can use the (array) type cast to convert an object to an equivalent associative array.

$data = array_map(function($x) { return (array)$x; }, $data);

If the original array of objects came from using json_decode(), you can tell it to return associative arrays instead of objects by giving it a true second argument.

$data = json_decode($data, true);

Multiline to associative array with PHP

You really can use a non-regex approach like

$s = 'FOO:317263
BAR:abcd
BAZ:s fsiu sfd sdf s dfsdddddd';

$a = array();
foreach (explode("\n", $s) as $line) {
$chnk = explode(':', $line, 2);
$a[$chnk[0]] = $chnk[1];
}
print_r($a);

After splitting with LF, explode(':', $line, 2); is used to split the line with the first occurrence of a colon.

If you can have different/mixed line endings, replace explode("\n", $s) with preg_split('~\R+~', $s) or even preg_split('~\R+~u', $s) if you deal with Unicode.

If you know you need to do some more matching than you revealed in the question, and you really need a regex, you may consider

$a = array();
if (preg_match_all('~^(\w+)\h*:\h*(.+)~m', $s, $matches)) {
$a = array_combine($matches[1],trim($matches[2]));
}
print_r($a);

See the PHP demo and the regex demo. Details:

  • ^ - start of a line (due to m flag)
  • (\w+) - Group 1: one or more word chars
  • \h*:\h* - a colon enclosed with zero or more horizontal whitespaces
  • (.+) - Group 2: the rest of the line.

You can also use parse_ini_string:

$a = parse_ini_string(preg_replace('/^([^:\v]*):(.*)/m', '$1=\'$2\'', $s), FALSE);

See this PHP demo.

The preg_replace('/^([^:\v]*):(.*)/m', '$1=\'$2\'', $s) part replaces all first : chars on each line with a =, wraps the parts after the first : with single quotes (to let parse_ini_string correctly handle additional =s), and parse_ini_string gets the array of keys and values.



Related Topics



Leave a reply



Submit