I have following array.
{#11950
+"attributes": array:3 [
0 => {#608
+"attribute_value": "test123"
+"attribute_name": "name"
}
1 => {#556
+"attribute_value": "foo moo"
+"attribute_name": "lastname"
}
2 => {#605
+"attribute_value": "sample moo"
+"attribute_name": "email"
}
3 => {#606
+"attribute_value": "holo"
+"attribute_name": "adress"
}
]
}
I would like to convert it like follow
$a = array(
'name' => 'test123',
'lastname' => 'foo moo',
'email' => 'sample moo',
'address' => 'holo
);
I will do same operation countless time so I believe there should be a proper solution rather then looping all values and checking out attribute_name with if else etc.
You can do this with a single call to array_column, by using the $index_key parameter:
$arr = [
['attribute_name' => 'foo', 'attribute_value' => 123],
['attribute_name' => 'bar', 'attribute_value' => 456],
['attribute_name' => 'baz', 'attribute_value' => 789],
];
$result = array_column($arr, 'attribute_value', 'attribute_name');
See https://eval.in/705641
Short but simple:
$result = array_combine(array_column($arr, 'attribute_name'), array_column($arr, 'attribute_value'));
You could do a simple foreach loop and build the result array :)
$result = [];
foreach ($array as $attr) {
$result[$attr['attribute_name']] = $attr['attribute_value'];
}
This reads a bit better. It probably doesn't make much different in most cases, but I don't think it's as performant as just a simple foreach ;)
$result = array_combine(
array_column($array, 'attribute_name'),
array_column($array, 'attribute_value')
);
Related
I have an array as key => value pair such as:
$array = [ 10 => 'Windows', 12 => 'Keyboard', 15 => 'Monitor' ];
What I would like to achieve without using any foreach or loops the following:
$converted = [
0 => [ 'id' => 10, 'name' => 'Windows'],
1 => [ 'id' => 12, 'name' => 'Keyboard'],
2 => [ 'id' => 15, 'name' => 'Monitor']
];
Here they indices in new array doesn't matter. Any tips??
No foreach and no loop, but now there is a closure:
$result = array_map(function ($id, $name) {
return [
'id' => $id,
'name' => $name
];
}, array_keys($array), array_values($array));
Even if there was a PHP function that did this exactly, it would be using a loop internally.
function do_what_ghazanfar_mir_wants(array $array) {
return array_map(function ($id, $name) {
return [
'id' => $id,
'name' => $name
];
}, array_keys($array), array_values($array));
}
And the single liner is:
$result = do_what_ghazanfar_mir_wants($array);
And the foreach approach for comparison:
$res = [];
foreach ($array as $id => $name) {
$res[] = [ 'id' => $id, 'name' => $name ];
}
If you want to keep it really short then array_walk will do it in one line:
array_walk($array, function(&$value, $key) { $value = ['id' => $key, 'name' => $value]; });
See https://3v4l.org/OEohi
But I think a foreach loop is probably going to be a lot more readable.
Do it with array_map(), Just pass the keys array_keys($array) and values $array as the parameter of your array_map()
<?php
$array = [ 10 => 'Windows', 12 => 'Keyboard', 15 => 'Monitor' ];
function map_by_key($m, $n)
{
return(array('id' => $m, 'name'=>$n));
}
$output = array_map("map_by_key", array_keys($array),$array);
print '<pre>';
print_r($output);
print '</pre>';
?>
DEMO: https://3v4l.org/iTVSm
I have the following array that includes id:
[Key1] => 1
[Key2] => 2, 3
I would like to replace these ids by their respective name from this second array:
[0] => Array
(
[ID] => 1
[Name] => Name1
)
[1] => Array
(
[ID] => 2
[Name] => Name2
)
[2] => Array
(
[ID] => 3
[Name] => Name3
The desired output:
[Key1] => Name1
[Key2] => Name2, Name3
I have the following code which works but I know this is not the right way. If anybody could let me know what would be a better way to achieve this, it would be greatly appreciated.
What my code looks like:
$var1 = explode(", ", $array1["Key1"]); // No need to explode in this example but "Key1" sometimes includes more than 1 ID
$var2 = explode(", ", $array1["Key2"]);
$array1["Key1"] = $var1 ; // This row is for the new array generated from "explode" to be a sub-array
$array1["Key2"] = $var2 ; // Same
for ($i = 0; $i < 83; $i++){
if($array1["Key1"][0] == $array2[$i]["ID"]){
$array1["Key1"][0] = $array2[$i]["Name"];
}
if($array1["Key1"][1] == $array2[$i]["ID"]){
$array1["Key1"][1] = $array2[$i]["Name"];
}
// (etc)
if($array1["Key2"][0] == $array2[$i]["ID"]){
$array1["Key2"][0] = $array2[$i]["Name"];
}
if($array1["Key2"][1] == $array2[$i]["ID"]){
$array1["Key2"][1] = $array2[$i]["Name"];
}
// (etc)
}
$var1 = implode(", ", $array1["Key1"]);
$var2 = implode(", ", $array1["Key2"]);
$array1["Key1"] = $var1 ;
$array1["Key2"] = $var2 ;
Just extract the ID and Name into a single-dimension and use it as search and replace parameters. We need to modify the IDs to search for and turn them into a pattern /\b$v\b/ where \b is a word boundary, so that 1 won't replace the 1 in 164 for example:
$replace = array_column($array2, 'Name', 'ID');
$search = array_map(function($v) { return "/\b$v\b/"; }, array_keys($replace));
$array1 = preg_replace($search, $replace, $array1);
You need to nest some loops. Here is a sample that should work:
//Processing Array
$arrayOne = array(
"Key1" => "1",
"Key2" => "2, 3");
//Lookup Array
$arrayTwo = array(
array(
"ID" => "1",
"Name" => "Name1"),
array(
"ID" => "2",
"Name" => "Name2"),
array(
"ID" => "3",
"Name" => "Name3"));
var_dump($arrayOne);
//Loop through all values in our original array
foreach($arrayOne as &$arrValue) {
//Split the value in the original array into another temporary array
//if there are multiple values.
$valueArray = explode(", ", $arrValue);
$outputArray = array();
foreach($valueArray as &$myValue) {
//Now do a lookup to replace each value
foreach($arrayTwo as &$lookupValue) {
//Find a match
if($myValue==$lookupValue["ID"]) {
$myValue = $lookupValue["Name"];
//We found the value we want, so let's break out of this loop
break;
}
}
//Append the value
array_push($outputArray, $myValue);
}
//Convert back to string
$arrValue= implode(", ", $outputArray);
}
var_dump($arrayOne);
There are improvements you could possibly make to this code if your incoming data was always sorted, but I imagine that is just the case for your sample above.
I have an approach to do this. You can make a try if you wish see here at:- https://eval.in/839823. I am using array_column to map the key=>value pair and then simple used foreach.
<?php
$main = ['Key1' => 1,'Key2' => '2, 3'];
$match = [
[
'ID' => 1,
'Name' => 'Name1'
],
[
'ID' => 2,
'Name' => 'Name2'
],
[
'ID' => 3,
'Name' => 'Name3'
]
];
$final_array=[];
$mapped = array_column($match, 'Name', 'ID');
foreach($main as $k=>$v){
$r = explode(',',$v);
if(count($r)>1){
$final_array[$k] = $mapped[$r[0]]. ", ".$mapped[intval($r[1])];
}else{
$final_array[$k] = $mapped[$r[0]];
}
}
print '<pre>';
//print_r($mapped);
print_r($final_array);
print '</pre>';
Output :
Array
(
[Key1] => Name1
[Key2] => Name2,Name3
)
Edit : As per comment of Josh Maag,
My code will only work if he only has a maximum of 2 values in Key2.
If Key3 contains "4,5,6" this code will leave the 6 untouched.
<?php
$main = ['Key1' => 1,'Key2' => '2,3','Key3' => '4,5,6'];
$match = [
[
'ID' => 1,
'Name' => 'Name1'
],
[
'ID' => 2,
'Name' => 'Name2'
],
[
'ID' => 3,
'Name' => 'Name3'
],
[
'ID' => 4,
'Name' => 'Name4'
],
[
'ID' => 5,
'Name' => 'Name5'
],
[
'ID' => 6,
'Name' => 'Name6'
]
];
$final_array=[];
$mapped = array_column($match, 'Name', 'ID');
foreach($main as $k=>$v){
$r = explode(',',$v);
if(count($r)>1){
$final_array[$k] = implode(',',array_map(function($key) use ($mapped){ return $mapped[$key]; }, array_values($r)));
}else{
$final_array[$k] = $mapped[$r[0]];
}
}
print '<pre>';
print_r($mapped);
print_r($final_array);
print '</pre>';
?>
See demo See here https://eval.in/839939
The core function that should be used for this task is preg_replace_callback(). Why? Because it is uniquely qualified to handle this operation in a single function call. It seems a tragedy to not use php functions for their designed purpose.
Beyond preg_replace_callback(), only array_column() is needed to prepare the $array2 data as a simple lookup array.
Code: (Demo)
$array1=["Key1"=>"1","Key2"=>"22, 4, 123"];
$array2=[["ID"=>"1","Name"=>"Name1"],["ID"=>"22","Name"=>"Name22"],["ID"=>"123","Name"=>"Name123"]];
$lookup=array_column($array2,'Name','ID'); // generate array: keys = IDs, vals = Names
$result=preg_replace_callback('/\d+/',function($m)use($lookup){return isset($lookup[$m[0]])?$lookup[$m[0]]:"*{$m[0]}*";},$array1);
var_export($result);
Output:
array (
'Key1' => 'Name1',
'Key2' => 'Name22, **4**, Name123',
)
There is no need to run any preparations (excluding $lookup) using additional loops or function calls.
This pattern will match all full ID numbers from each element in $array1 and process them individual. Each numeric match is sent to the anonymous callback function to receive its customized replacement string -- delivered by the $lookup data.
As an additional consideration, I have included an asterisk-wrapped replacement when an ID is not found in $lookup.
I have following PHP code, to create JSON with foreach out of an array:
$array = array('one', 'two', 'three', 'four');
foreach ($array as $key => $value) {
$temp = array(
'text' => 'Hello',
'text1' => 5,
'collect' => array(
$value => array(
'xx' => 'yy',
'key' => $key
)
)
);
echo json_encode($temp);
}
The Output is this:
{
"text":"Hello",
"text1":5,
"collect":{"one":{"xx":"yy","key":0}}
}
{
"text":"Hello",
"text1":5,
"collect":{"two":{"xx":"yy","key":1}}
}
{
"text":"Hello",
"text1":5,
"collect":{"three":{"xx":"yy","key":2}}
}
{
"text":"Hello",
"text1":5,
"collect":{"four":{"xx":"yy","key":3}}
}
But i want this:
{
"text":"Hello",
"text1":5,
"collect": {
"one":{"xx":"yy","key":0},
"two":{"xx":"yy","key":1},
"three":{"xx":"yy","key":2},
"four":{"xx":"yy","key":3}
}
}
I get single 4 single JSON Objects, but i need only one with an collect object.
I don't get it...
I'd like to educate readers on a couple alternative methods as well as highlight that the other two answers needlessly instantiate the collect subarray prior to the loop (Sahil's answer does this twice for some reason).
The initial input array and the static elements of the result array should be placed at the start as the other answers do. Purely due to personal preference, I'll be using short array syntax.
Inputs:
$array=['one','two','three','four'];
$result=['text'=>'Hello','text1'=>5]; // <-- no 'comment' element declared
Now for the different methods that traverse $array and build the dynamic elements of the result.
Method #1: array_walk()
array_walk($array,function($v,$k)use(&$result){
$result['collect'][$v]=['xx'=>'yy','key'=>$k];
});
Method #2: array_map()
array_map(function($k,$v)use(&$result){
$result['collect'][$v]=['xx'=>'yy','key'=>$k];
},array_keys($array),$array);
Array map is less efficient because it requires an additional array to be passed to the function.
Method #3: foreach()
foreach($array as $k=>$v){
$result['collect'][$v]=['xx'=>'yy','key'=>$k];
}
$result at this point looks like this:
array (
'text' => 'Hello',
'text1' => 5,
'collect' => array (
'one' => array ( 'xx' => 'yy', 'key' => 0 ),
'two' => array ( 'xx' => 'yy', 'key' => 1 ),
'three' => array ( 'xx' => 'yy', 'key' => 2 ),
'four' => array ( 'xx' => 'yy', 'key' => 3 )
)
)
foreach() is the simplest and easiest to read for this case, but it important to understand and compare versus php's array functions to ensure you are using the best method for any given project.
For anyone wondering what the & is doing in use(&$result), that is a reference which is used in the anonymous function (aka closure) to make the $result variable "modifiable" within the function.
Finally convert to json using json_encode() and display with echo:
echo json_encode($result);
All of the above methods product the same desired output:
{"text":"Hello","text1":5,"collect":{"one":{"xx":"yy","key":0},"two":{"xx":"yy","key":1},"three":{"xx":"yy","key":2},"four":{"xx":"yy","key":3}}}
Here is the Demo of all three methods
Try this simple code ,in which we have a declaration before foreach.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$array = array('one', 'two', 'three', 'four');
$temp = array(
'text' => 'Hello',
'text1' => 5,
'collect' => array()
);
$collect = array();
foreach ($array as $key => $value)
{
$collect[$value] = array(
'xx' => 'yy',
'key' => $key
);
}
$temp["collect"]=$collect;
echo json_encode($temp);
Output:
{
"text": "Hello",
"text1": 5,
"collect": {
"one": {
"xx": "yy",
"key": 0
},
"two": {
"xx": "yy",
"key": 1
},
"three": {
"xx": "yy",
"key": 2
},
"four": {
"xx": "yy",
"key": 3
}
}
}
You need to loop through and append these value arrays to the 'collect' key of your temp array.
$array = array('one', 'two', 'three', 'four');
$temp = array(
'text' => 'Hello',
'text1' => 5,
'collect' => array()
);
foreach ($array as $key => $value) {
$temp['collect'][$value] = array(
'xx' => 'yy',
'key' => $key
);
}
echo json_encode($temp);
Here is the demo: https://eval.in/788929
$arr[] = array('title' => 'Overview');
$arr[] =array('title' => 'General');
$arr[] =array('title' => 'History');
$arr[] =array('title' => 'Construction');
$arr[] =array('title' => 'Plan');
$arr[] =array('title' => 'Other');
$info_arr[] = array("title" => "General", text => "value1");
$info_arr[] = array("title" => "History", text => "value1");
$info_arr[] = array("title" => "Construction", text => "value1");
$info_arr[] = array("title" => "Plan", text => "value1");
I need to be able merge these arrays together.So they look something like this. As I will need to loop thru the consolidated array. Other, Overview do not have any text values but still need to placed into the array.
$new_arr[] = array("title" => "General", text => "value1", "title" => "History", text => "value1", "title" => "Construction", text => "value1"
,"title" => "Plan", text => "value1","title" => "Overview", text => "","title" => "Other", text => "");
I have tried for loops (using count value), foreach loops, I thought array_intersect or array_diff don't see to solve the issue. This should not be so difficult, but I'm trying to piece together some really bad legacy code. Or the cube/florescent lights might have finally got to me.
Update:
while ($stmt->fetch()) {
$arr[] = array("title" => $Title);
}
and
while ($dstmt->fetch()) {
$info_arr[] = array("title" => $descriptionType, "descriptiontext" => $descriptionText); , "descriptiontext" => $val );
}
$dstmt & $stmt are queries.
I thought this would work but not so much
$r = array_intersect($arr, $info_arr);
var_dump($r);
Something like this Let me clarify:
$new_arr = array(array("title" => "General", text => "value1"),
array("title" => "History", text => "value1"),
array("title" => "Construction", text => "value1"),
array("title" => "Plan", text => "value1"),
array("title" => "Overview", text => ""),
array("title" => "Other", text => "")
);
If you want to work with these two arrays, you can just use the title as the key in $r.
foreach (array_merge($arr, $info_arr) as $x) {
$r[$x['title']]['title'] = $x['title'];
$r[$x['title']]['text'] = isset($x['text']) ? $x['text'] : '';
}
Or, you can go back a step and avoid having separate arrays by building the $r array in the same manner as you fetch your query results:
while ($stmt->fetch()) {
$r[$Title] = array('title' => $Title, 'text' => '');
}
while ($dstmt->fetch()) {
$r[$descriptionType] = array("title" => $descriptionType, "text" => $descriptionText);
}
Or, ideally, you could go back another step and avoid having separate queries by using a JOIN to get the same results in one query, but there's nothing in the question on which to base any specific suggestion for that.
As stated in the comments, the exact $new_arr you're requesting isn't possible. However, I think this will give you a similar result that should work for your purposes:
foreach ($info_arr as $infoArray) {
$found = array_search(array('title' => $infoArray['title']), $arr);
if (false !== false) {
unset($arr[$found]);
}
}
$new_arr = array_merge($info_arr, $arr);
It works by removing the "duplicates" from the original $arr before doing the array_merge().
If it's important to add an empty text value for the remaining items in $arr, do this before the array_merge():
foreach ($arr as &$arrArray) {
$arrArray['text'] = '';
}
The resulting array will look like this:
$new_arr[] = array(
array(
'title' => 'General',
'text' => 'value1',
),
array(
'title' => 'History',
'text' => 'value1',
),
array(
'title' => 'Construction',
'text' => 'value1',
),
array(
'title' => 'Plan',
'text' => 'value1',
),
array(
'title' => 'Overview',
'text' => '',
),
array(
'title' => 'Other',
'text' => '',
),
);
I want to know that is there a way to insert certain elements of an array into a new array. I mean I have an array containing 10 objects. Each object has 3 or four fields for example id, name , age , username. now I want to insert the id's of all the objects into the new array with a single call.Is there anyway to do that.
$array = [
[0] => [
id =>
name =>
],
[1] = > [
id =>
name =>
]
]
and so on now I want to insert all the id's of all the object into a new array with a single call. Is there a way to do that?
Use array_map() function.
Here is your solution:-
$ids = array_map( function( $arr ){
return $arr["id"];
}, $arr );
echo '<pre>'; print_r($ids);
A basic foreach loop will do the job just fine
$firstArray = array(
array(
'id' => 1,
'name' => 'abc'
),
array(
'id' => 2,
'name' => 'def'
),
array(
'id' => 3,
'name' => 'gh'
)
);
$onlyIds = array();
$onlyKeys = array();
//To get the array value 'id'
foreach($firstArray as $value){
$onlyIds[] = $value['id'];
}
//To get the array keys
foreach($firstArray as $key => $value){
$onlyKeys[] = $key;
}
You could use array_walk which could be considered a "single call"
$array = array(0 => array('id', 'name', 'age'), 1 => array('id', 'name', 'age'));
array_walk($array, function($item, $key) {
// $key is 0 or 1
// $item is either id, name, age
});
You can use array_column.
$arr = [ ['id' => 1, 'username' => 'a'], ['id' => 2, 'username' => 'b'] ];
$ids = array_column($arr, 'id')
$ids == [1, 2]