PHP add key value to array with each loop iteration - php

I have an array from which I want to create a new array with key value pairs. I think I know what's required, I just need some help with the syntax.
foreach ($stuff as $thing){
$thing1 = $thing->prop1;
$thing2 = $thing->prop2;
// this is where I need to set $newstuff = array($thing1 => $thing2);
$newstuff[] = ??
}

Can use array_map() instead of foreach(). Example:
$newstuff = array_map(function($v){return array($v->prop1=>$v->prop2);}, $stuff);
And using foreach():
foreach ($stuff as $thing){
$newstuff[] = array($thing->prop1=>$thing->prop2);
}

Do it like this:
foreach ($stuff as $thing){
$thing1 = $thing->prop1;
$thing2 = $thing->prop2;
// this is where I need to set $newstuff = array($thing1 => $thing2);
$newstuff[$thing1] = $thing2;
}

$newstuff = array();
foreach ($stuff as $thing){
$thing1 = $thing->prop1;
$thing2 = $thing->prop2;
// this is where I need to set $newstuff = array($thing1 => $thing2);
$newstuff[] = array($thing1 => $thing2);
}
or
$newstuff = array();
foreach ($stuff as $thing){
$thing1 = $thing->prop1;
$thing2 = $thing->prop2;
// this is where I need to set $newstuff = array($thing1 => $thing2);
$newstuff[$thing1] = $thing2;
}
depends on desired result...

$newstuff = array();
foreach ($stuff in $thing) {
$newstuff[$thing->prop1] = $thing->prop2;
}
or
$newstuff = array();
foreach ($stuff in $thing) {
$newstuff[] = array($thing->prop1, $thing->prop2);
}
All depends if you want to save in a array or not.

Related

Yii2 merge arrays

I need merge arrays for json API. I have 3 requests:
$data['winners'] = Winners::find()->limit(8)->all();
$user = User::findIdentity($value->user_id);
$product = Products::find()->localized($lang)->where(['coupon' => $value->coupon])->one();
I need foreach all $data['winners'], get User with id from $data['winners'], and get Product from $data['winners']. After i need merge in 1 json all my data.
I try like this:
$data['winners'] = Winners::find()->limit(8)->all();
foreach ($data['winners'] as $value){
$user = User::findIdentity($value->user_id);
$product = Products::find()->localized($lang)->where(['coupon' => $value->coupon])->one();
$result = ArrayHelper::merge($product, $user);
}
There is an even more elegant solution in PHP:
$winners = Winners::find()->limit(8)->all();
foreach ($winners as $value){
$user = User::findIdentity($value->user_id);
$product = Products::find()->localized($lang)
->where(['coupon' => $value->coupon])->one();
$data[] = [
'image'=> $product->prize_image,
'user' => $user->firstname.' '.$user->lastname,
'title' => $product->win_title,
];
}
return $data;
The PHP array operator [] in $data[] adds an array element at the end of the $data array.
If i understand correctly you question you need an array with all the $data[winners] istances and foreach instance merge the related Produce and User
then you could use
$result[] = array_merge($value, $product, $user);
.
$data['winners'] = Winners::find()->limit(8)->all();
foreach ($data['winners'] as $value){
$user = User::findIdentity($value->user_id);
$product = Products::find()->localized($lang)->where(['coupon' => $value->coupon])->one();
$result[] = array_merge($value, $product, $user);
}
I found solution:
$winners = Winners::find()->limit(8)->all();
$i = 0;
foreach ($winners as $value){
$user = User::findIdentity($value->user_id);
$product = Products::find()->localized($lang)->where(['coupon' => $value->coupon])->one();
$data[$i]['image'] = $product->prize_image;
$data[$i]['user'] = $user->firstname.' '.$user->lastname;
$data[$i]['title'] = $product->win_title;
$i++;
}
return $data;

How to compare two values from different foreach loop in PHP?

Here is My codes,
My question is if $company_id from foreach one equal to $Company_id from foreach two then echo company_name.
$ids = array();
$x = array();
$a = array();
foreach($companieslist as $keys=>$company) {
$x[$company->company_id] = [
'id' => $company->company_id,
'name' => $company->company_name
];
}
$entry = $a[$id];
foreach($uploads as $keys=>$general){
$ids[] = $general->Contract_Id;
$c_id = $general->Company_id;
....
Just talking from the performance side, what you should do is extract the company ids from the second batch to an array first, like this
$companies = array();
foreach ( $uploads as $keys => $general ) {
array_push( $companies, $general->Company_id );
}
Now, in the first foreach loop, you can just check if the company id exists in this $companies array, and then decide what to do
foreach($companieslist as $keys=>$company){
if(in_array($company->company_id,$companies)){
echo "Found {$company->company_id}<br/>\n";
}
}

add key pair value in array in foreach loop PHP

I want to make an key pair array in foreach loop in php. In my foreach loop i have city name and user name. I want to add all users for same city in array.
Ex [{city=>'pune',users=>("a","b","c","d")},{'city=>nk',users=>("e","b","c","f")}] or any other array format.
foreach ($studsInfo as $value) {
$studId = "".$value['_id'];
$indDetail = $industryM->getAllIndustries($studId);
$indusArray['industry'] = iterator_to_array($indDetail);
$city = $value['city'];
$name = $value['firstname'];
}
How could I add all name in array for same city.
Thankx in advance, Any suggestion and editing are welcome
You can use the city as the key users from the city as value.
$arr = [];
foreach ($studsInfo as $value) {
$studId = "".$value['_id'];
$indDetail = $industryM->getAllIndustries($studId);
$indusArray['industry'] = iterator_to_array($indDetail);
//$city = $value['city'];
//$name = $value['firstname'];
$arr[$value['city'][] = $value['firstname'];
}
foreach($arr as $k => $v) {
$result[] = array('city' => $k, 'users' => $v);
}

How to parse array from MySQL column in PHP

I have column extra_fields in table.
In column i have array, and I need to parse it
[{"id":"1","value":"fdsds"},{"id":"2","value":"\/images\/powered_by.png"},{"id":"3","value":"fdsfdsfdsfds"},{"id":"4","value":"<p>fdsfdsfdsfds<\/p>"}]
Try to do it with this code,
$extrafields = array();
foreach($this->item->extra_fields as $item)
{
$extrafields[$item->id] = $item->value;
}
but return empty string or...I don't know what
it is unclear as to what you want to achieve but here you go:
$test = json_decode('[{"id":"1","value":"fdsds"},{"id":"2","value":"\/images\/powered_by.png"},{"id":"3","value":"fdsfdsfdsfds"},{"id":"4","value":"<p>fdsfdsfdsfds<\/p>"}]');
$extrafields = array();
foreach ($test as $key => $item) {
$extrafields[$item->id] = $item->value;
}

merging overlapping array in php

I have two array like below:
$array1 = array(
[0]=>array([0]=>a_a [1]=>aa)
[1]=>array([0]=>b_b [1]=>bb)
[3]=>array([0]=>c_c [1]=>cc)
)
$array2 = array(
[0]=>array([0]=>aa [1]=>AA)
[1]=>array([0]=>bb [1]=>BB)
[3]=>array([0]=>cc [1]=>CC)
)
what i would like to merge or overlap to output like below:
$result = array(
[0]=>array([0]=>a_a [1]=>AA)
[1]=>array([0]=>b_b [1]=>BB)
[3]=>array([0]=>c_c [1]=>CC)
)
either output like below:
$result = array(
[0]=>array([0]=>a_a [1]=>aa [2]=>AA)
[1]=>array([0]=>b_b [1]=>bb [2]=>AA)
[3]=>array([0]=>c_c [1]=>cc [2]=>AA)
)
how i do this thing what is the best way any suggestion.
I don;t know which is the best way but you could do this with two loops. Example:
$result = array();
foreach($array1 as $val1) {
foreach($array2 as $val2) {
if($val1[1] == $val2[0]) {
$result[] = array($val1[0], $val1[1], $val2[1]);
}
}
}
echo '<pre>';
print_r($result);
For the first result, its easily modifiable:
$result[] = array($val1[0], $val2[1]);
you can use this function
1st output :
function my_array_merge(&$array1, &$array2) {
$result = array();
foreach($array1 as $key => &$value) {
$result[$key] = array_unique(array_merge($value, $array2[$key]));
}
return $result;
}
$arr = my_array_merge($array1, $array2);
2st output :
function my_array_merge(&$array1, &$array2) {
$result = array();
foreach($array1 as $key => &$value) {
$result[$key] = array_merge(array_diff($value, $array2[$key]), array_diff($array2[$key],$value));
}
return $result;
}

Categories