This question already has answers here:
Merge two arrays as key value pairs in PHP
(3 answers)
Closed 7 years ago.
Given these two array:
$name=array("alice","ken","wendy");
$frequent=array(3,6,9);
I try to combine it like
$data = array($name=>$frequent);
but it fails. Anyone can help?
I want this:
$data = array(
'alice' => 3,
'ken' => 6,
'wendy' => 9,
);
You can use array_combine
$combined_array = array_combine($name, $frequent);
Documentation here: http://php.net/manual/en/function.array-combine.php
You can use array_combine function as
Syntax:
array_combine ( array $keys , array $values )
So yours is like
$name=array("alice","ken","wendy");
$frequent=array(3,6,9);
$result = array_combine($name,$frequent);
Output
Array
(
[alice] => 3
[ken] => 6
[wendy] => 9
)
[akshay#localhost tmp]$ cat test.php
<?php
$name=array("alice","ken","wendy");
$frequent=array(3,6,9);
// One easy way is
print_r( array_combine($name, $frequent) );
// Another lengthy way
while ( ($key = array_shift($name)) && ($value = array_shift($frequent)) )
{
$combined[$key] = $value;
}
print_r( $combined );
?>
Output
[akshay#localhost tmp]$ php test.php
Array
(
[alice] => 3
[ken] => 6
[wendy] => 9
)
Array
(
[alice] => 3
[ken] => 6
[wendy] => 9
)
write like this $combined_array = array_combine($name, $frequent);
If you want to do it manually.
<?php
$name=array("alice","ken","wendy");
$frequent=array(3,6,9);
$combined=array();
for($i=0; $i<3; $i++)
{
$combined[$name[$i]]=$frequent[$i];
}
var_dump($combined);
?>
Related
This question already has answers here:
Return single column from a multi-dimensional array [duplicate]
(7 answers)
How to implode array indexes?
(1 answer)
Closed 3 years ago.
I have an array that I am trying to convert into two strings, one with dates and one with the data values. This is a sample array:
[$output Array below]
Array
(
[2019-03-19] => Array
(
[data_values] => 1566
)
[2019-03-18] => Array
(
[data_values] => 1542
)
[2019-03-17] => Array
(
[data_values] => 786
)
[2019-03-16] => Array
(
[data_values] => 756
)
)
A desired output would be something like:
$dates = '2019-03-19,2019-03-18,2019-03-17,2019-03-16';
$data_values = '1566,1542,786,756';
I've tried this, which will give me the data_values but I can't get the dates, I assume because its the array key?
function implode_r($g, $p) {
return is_array($p) ?
implode($g, array_map(__FUNCTION__, array_fill(0, count($p), $g), $p)) :
$p;
}
$data_values = implode_r(',', $output);
echo $data_values;
You can just use array_keys and array_column:
$dates = implode(',', array_keys($output));
$data_values = implode(',', array_column($output, 'data_values'));
Demo on 3v4l.org
This will give you the answer you expect:
<?php
// Build strings based on array
function implode_r($output) {
$dates = $data_values = "";
if(is_array($output)){
$dates=implode(',',array_keys($output));
$data_values=implode(',',array_column($output, 'data_values'));
}
return compact('dates', 'data_values');
}
// Example usage
$output = [
"2019-03-17" => ["data_values" => 1],
"2019-03-18" => ["data_values" => 2],
"2019-03-19" => ["data_values" => 3],
"2019-03-20" => ["data_values" => 4]
];
$result = implode_r($output);
echo $result['dates'];
echo $result['data_values'];
This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 6 years ago.
I have an array that looks like this:
Array (
[0] => Array ([order_variable_key] => surname [order_variable_value] => Hudsons )
[1] => Array ( [order_variable_key] => number [order_variable_value] => 13 )
[2] => Array ( [order_variable_key] => firstname [order_variable_value] => Dave )
)
I want to convert it to an array that looks like this:
Array(
'surname' => Hudsons,
'number' => 13,
'firstname' => Dave);
I managed to isolate the values but was not able to pair them with eachother.
I want to pair the values of the nested array with eachother.
$new_array= array();
foreach($array_name1 as $key=>$val){
$new_array[$val['order_variable_key']] = $val['order_variable_value'];
}
you can use
$newArray = array();
foreach($array as $obj)
{
$newArray[$obj['order_variable_key']] = $obj['order_variable_value']
}
Try this:
$arr = // your array;
$data = array();
foreach($arr as $val)
{
$arr[$val['order_variable_key']] = $val['order_variable_value'];
}
print_r($arr);
will give your desired format.
This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
I have the following array:
Array
(
[id] => 1043847
[company] => Array
(
[businesstype] => Array
(
[0] => Array
(
[id] => 6
[name] => Business Service Provider
)
[1] => Array
(
[id] => 8
[name] => Retailer
)
)
)
)
I would like to be able to get the value of company -> businesstype -> name for all the arrays inside businesstype so it would display like this:
Business Type(s): Business Service Provider, Retailer
Right now if I do this:
<?php echo($chamber_member['company']['businesstype']['name']); ?>
Nothing happens, if I do this:
<?php echo($chamber_member['company']['businesstype'][0]['name']); ?>
I get only the one for array [0]. How can I get both values separated by a comma like on the example above? Any help is appreciated, thank you in advance!!
This should work for you:
First get the column into an array with array_column(), then you can simply implode() this array ,e.g.
echo implode(",", array_column($chamber_member["company"]["businesstype"], "name"));
foreach($chamber_member['company']['businesstype'] as $a)
$b[] = $a['name'];
$result = implode(", ", $b);
Take names by array_column and cobine by implode
<?php echo implode(',', array_column($chamber_member['company']['businesstype'], 'name')); ?>
Try to loop into the array position with the 'businesstype' values:
for($i = 0; $i < count($chamber_member['company']['businesstype']); $i++){
echo $chamber_member['company']['businesstype'][$i]['name'] . ', ';
}
I like array_column() but for completeness, just foreach():
foreach($chamber_member['company']['businesstype'] as $array) {
echo $array['name'];
}
Or add to an array and implode().
Another solution using array_map():-
$arr=array
(
'id' => 1043847,
'company' => array
(
'businesstype' => array
(
array
(
'id'=> 6 ,
'name' => 'Business Service Provider'
),
array
(
'id' => 8,
'name' => 'Retailer'
)
)
)
);
echo $names = implode(',',array_map( function($person) { return $person['name']; },$arr['company']['businesstype']));
Use this. best answer for this..
$result=$chamber_member['company']['businesstype'];
$last=end($result);
for($i=0;$i<count($result);$i++)
{
$b[$i] = $result[$i]['name']; echo $b[$i]; if($last!=$result[$i]['name']){ echo ","; }
}
This question already has answers here:
How do I retrieve a single column from mysqli results
(5 answers)
Closed 6 months ago.
I am currently using a mysql_query with the UNION function. This is the array that I get:
Array
(
[0] => bob
[added] => bob
)
Array
(
[0] => test1
[added] => test1
)
Is there a way that I can take this array, merge it, remove the added values, place the data in numerical order and make it look like this?:
Array
(
[0] => bob
[1] => test1
)
I know somebody'll ask me what have I done so far. Honestly, I have no idea where to go from here.
array_reduce(
array_map(function($i) {
return $i[0];
}, $data),
function($result, $item) {
$result[] = $item;
return $result;
},
array()
);
or
call_user_func_array('array_merge',
array_map(function($i) {
return $i[0];
}, $data)
);
$array1=array_unique($array1);
$array2=array_unique($array2);
$result = array_merge ($array1,$array2);
When you are fetching the data you can create your array eg:
while($row = mysqli_fetch_array($result, MYSQLI_NUM){
$newArray[] = $row[0];
}
and from your current array you can do
$newArray = array();
foreach($array as $value){
$newArray = array_push($newArray,$value[0]);
}
This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 6 months ago.
A simple thing to do, but I forgot how to convert
Array
(
[0] => Array
(
[hashcode] => 952316176c1266c7ef1674e790375419
)
[1] => Array
(
[hashcode] => 5b821a14c98302ac40de3bdd77a37ceq
)
)
into this:
Array (952316176c1266c7ef1674e790375419, 5b821a14c98302ac40de3bdd77a37ceq)
I know this is premature but since this is coming soon I figured I throw this out there. As of (the not yet released) PHP 5.5 you can use array_column():
$hashcodes = array_column($array, 'hashcode');
Try this :
$array = array(array("test"=>"xcxccx"),array("test"=>"sdfsdfds"));
$result = call_user_func_array('array_merge', array_map("array_values",$array));
echo "<pre>";
print_r($result);
Output:
Array
(
[0] => xcxccx
[1] => sdfsdfds
)
A good ol' loop solves :)
<?php
$array = array(
array( 'hashcode' => 'hash' ),
array( 'hashcode' => 'hash2' ),
);
$flat = array();
foreach ( $array as $arr ) {
$flat[] = $arr['hashcode'];
}
echo "<pre>";
print_r( $flat );
?>
$source = array(
array(
'hashcode' => '952316176c1266c7ef1674e790375419'
),
array(
'hashcode' => '5b821a14c98302ac40de3bdd77a37ceq'
)
);
$result = array();
array_walk($source, function($element) use(&$result){
$result[] = $element['hashcode'];
});
echo '<pre>';
var_dump($result);