I have an array which contains data from the database:
$responses = FormResponses::where('form_id', '>=', $phoneFirstElement->id)->where('form_id', '<=', $phoneLastElement->id)->get();
$responsesArray = $responses->toArray();
Then my nested loop:
foreach ($phone as $key => $value2) // Populate Phone Numbers Horizontally
{
$sheet->cell($start.'9', $value2->phone);
// This will fill the responses for each number
foreach ($metrics as $key => $value)
{
$form_id = $value2->id;
$metrics_id = $value->id;
$neededObjects = array_filter(
$responsesArray,
function ($e) use ($form_id, $metrics_id) {
return $e['form_id'] == $form_id && $e['metrics_id'] == $metrics_id;
}
);
var_dump($neededObjects);
exit;
//$responses = FormResponses::where('form_id', '=', $value2->id)->where('metrics_id', '=', $value->id)->get();
//$sheet->cell($start.$count, $neededObjects[$counter]['response']);
$sheet->cell('C'.$count, $value->question);
$sheet->cell('B'.$count, $value->description);
$sheet->cell('A'.$count, $value->metrics_name);
$counter++;
$count++;
$neededObjects = array();
}
$start++;
$count = 10;
//$counter = 0;
}
My $responsesArray is the data which has lots of record, so I want to extract those data to get the specific one I need using array_filter and storing it in
$neededObjects
However when I make a var_dump I get something like
array (size=1)
0 =>
array (size=7)
'id' => int 141730
'form_id' => int 4430
'metrics_id' => int 1
'response' => string 'Yes' (length=3)
'remarks' => string '' (length=0)
'created_at' => string '2015-11-23 19:30:07' (length=19)
'updated_at' => string '2015-11-23 19:30:07' (length=19)
Then when the next records loops in
array (size=1)
1 =>
array (size=7)
'id' => int 141731
'form_id' => int 4430
'metrics_id' => int 2
'response' => string 'Yes' (length=3)
'remarks' => string '' (length=0)
'created_at' => string '2015-11-23 19:30:07' (length=19)
'updated_at' => string '2015-11-23 19:30:07' (length=19)
I don't understand why it is increamenting. The first one is 0 then next is 1 . Yes given that I am making an array_filter inside a nested loop but I didn't put any inreament vairables to it and the array filter will aways give me 1 record of data so I am expecting it atleast to be in index 0 or just a format like this:
array (size=7)
'id' => int 141731
'form_id' => int 4430
'metrics_id' => int 2
'response' => string 'Yes' (length=3)
'remarks' => string '' (length=0)
'created_at' => string '2015-11-23 19:30:07' (length=19)
'updated_at' => string '2015-11-23 19:30:07' (length=19)
Any results that are returned by array_filter maintain their old keys. If you want the keys to reset then apply array_values to the result.
If you only want one result then call current() or array_pop() on the result to get the first record.
Even if array_filter returns one record, it will still be in the original array with the same key.
array_filter returns the array elements with their keys intact depending upon your filter function. So the keys (0, 1 etc) are the keys in your original array. Use array_pop to get the only element in your array.
Related
I have two arrays like:
array (size=4)
0 => string '5' (length=1)
1 => string '4' (length=1)
2 => string '2' (length=1)
3 => string '2' (length=1)
3 => string '8' (length=1)
and one array more that I load from an XML file:
object(SimpleXMLElement)[1]
public 'book' =>
array (size=101)
0 =>
object(SimpleXMLElement)[2]
public 'id' => string '1' (length=1)
public 'title' => string 'p' (length=1)
1 =>
object(SimpleXMLElement)[3]
public 'id' => string '2' (length=1)
public 'title' => string 'pp' (length=2)
2 =>
object(SimpleXMLElement)[4]
public 'id' => string '3' (length=1)
public 'title' => string 'pen' (length=3)
3 =>
object(SimpleXMLElement)[5]
public 'id' => string '4' (length=1)
public 'title' => string 'lapton' (length=6)
......
......
101 =>
object(SimpleXMLElement)[103]
public 'id' => string '101' (length=1)
public 'title' => string 'title' (length=5)
I want to compare each value of key id of second array with key of first array for each value. When it's the same, I want to update value of key title of second array.
Assuming your first array is $idArray and your second is $xmlArray, you could use something like this.
$result = array_map(function($xmlElement) use ($idArray) {
if (in_array($xmlElement->id, $idArray)) {
$xmlElement->title = 'updated value';
}
return $xmlElement;
}, $xmlArray);
Assumptions
the first array is called $array1
the second array is called $fromXML
the second array is not actually an array, it's a SimpleXMLElement with the following structure (psuedocode / JSONish syntax)
{
'book' => {
0 => SimpleXMLElement {
'id' => 1,
'title' => 'p'
}
}
}
I assume you can access the second array of elements with $fromXML['book']
I assume you can access an attribute of the first element with $fromXML['book'][0]['id']
I assume that you can set the text of the title of the first element with $fromXML['book'][0]['title'][0] = 'new title'
based on How can I set text value of SimpleXmlElement without using its parent? and PHP SimpleXML, how to set attributes? and PHP foreach change original array values
Solution
foreach($fromXML['book'] as $key => $element) {
if(array_key_exists($element['id'], $array1)) {
$fromXML['book'][$key]['title'][0] = $array1[$element->id];
}
}
Caveat and troubleshooting
I didn't test this, just going off of the documentation. If I've misinterpreted the structure of your SimpleXMLElement array, try experimenting with var_dump($fromXML['some']['key']) until you find the right way to access the array/element
Note: Apparently, array_key_exists() performs better than in_array() on large arrays
Try this for now
foreach($array1 as $arr1 => $val1){
foreach($array2 as $arr2 =>$val2){
if($arr1==$arr2){
$val2['title']='update value';
}
}
}
Currently I have this:
$pattern = array('industry_id','category_id','subcategory_id');
$data = array('advert_id' => string '261501' (length=6)
'advert_type_id' => string '7' (length=1)
'user_id' => string '6221' (length=4)
'industry_id' => string '17' (length=2)
'category_id' => string '769' (length=3)
'subcategory_id' => string '868' (length=3)
'model' => string 'Custom Semi Drop Deck Trailer' (length=29)
'description' => string 'Industry: Trailer );
Then:
array_intersect_key( $data , array_flip($pattern) );
Using array_interect_key & array_flip to get the values from $data based on $pattern, I will get a result like this:
array (size=3)
'category_id' => string '769' (length=3)
'subcategory_id' => string '930' (length=3)
'industry_id' => string '17' (length=2)
Unfortunately as you can see the result key sorting is not the same that I declared in $pattern. Is there a shorthand way to sort it like I declared in $pattern because after this I want to implode the array and do something like this industry_id.category_id.subcategory_id without hard coding the keys.
Since you already figured out array_intersect_key method which will not get you the desired key ordering of $pattern, try this instead:
// since original $pattern is not ASSOC (only vals)
// flip it so defined vals become keys
$pattern_flipped = array_flip($pattern);
$result = array();
foreach ($pattern_flipped as $k => $v) {
if (isset($data[$k])) {
$result[$k] = $data[$k];
}
}
var_dump($result); // test
// can use original 0 1 2 dynamic keys for concatenation
echo $result[$pattern[0]], $result[$pattern[1]], $result[$pattern[2]], '<br>';
// or use hardcoded keys
echo $result['industry_id'], $result['category_id'], $result['subcategory_id'], '<br>';
You know I'm not sure how you're getting the result you describe. I've tried your code and I get
array (size=3)
'industry_id' => string '17' (length=2)
'category_id' => string '769' (length=3)
'subcategory_id' => string '868' (length=3)
You could do this another way though using array_filter
$newData = array_filter($data, function($key) use ($pattern) {
if (in_array($key, $pattern))
return true;
}, ARRAY_FILTER_USE_KEY)
I tried merging two arrays if the key is not exists in the array but I cannot accomplish this. How can i do? This is what I have tried:
array (size=3) // name of the array $exchange
'purchase' => string '1' (length=1)
'agriculture' => string '1' (length=1)
array (size=6) // name of the array $fixed
'purchase' => string '0' (length=1)
'ICT' => string '0' (length=1)
'agriculture' => string '0' (length=1)
'entertainment' => string '0' (length=1)
'goods and service' => string '0' (length=1)
'other' => string '0' (length=1)
foreach($fixed as $keys=>$values){
if(!in_array($values, $exchange, true)){
array_push($exchange, $keys);
}
}
I get this result:
array (size=7)
'ICT' => string '1' (length=1)
0 => string 'purchase' (length=8)
1 => string 'ICT' (length=3)
2 => string 'agriculture' (length=11)
3 => string 'entertainment' (length=13)
4 => string 'goods and service' (length=17)
5 => string 'other' (length=5)
But I want:
array (size=7)
'ICT' => string '1' (length=1)
'purchase' => string '0' (length=8)
'agriculture' => string '0' (length=11)
'entertainment' => string '0' (length=13)
'goods and service' => string '0' (length=17)
'other' => string '0' (length=5)
Try this:
foreach ($fixed as $keys => $values) {
if (! array_key_exists($keys, $exchange)) {
$exchange[$keys] = $values;
}
}
It checks if the key $keys does not already exists in $exchange and adds it together with its value ($values).
Or you can simply replace the entire foreach() block with:
$exchange = $exchange + $fixed;
The addition $exchange + $fixed adds to $exchange the keys (and their values) that are in $fixed but are not in $exchange. The combined array is then stored in $exchange.
Read also this answer. It explains where your code is wrong.
There's a couple of problems with this:
in_array will search the values of the given array, not the keys, so in this case you just end up checking if the value "0" exists in the array $variable. I assume instead you wanted to check if the key already exists.
array_push pushes a new value into the next available key.
What you need is to check if a key exists, and if not, add it with the existing value. Try this instead:
for($fixed as $key=>$value)
{
if(!isset($exchange[$key])) // Check if the key exists
{
$exchange[$key] = $value; // Add the new key -> value pair.
}
}
Try with array_merge (http://php.net/manual/es/function.array-merge.php)
$result = array_merge($array1, $array2);
You can do it in several ways depending the result you desire:
foreach($fixed as $key=>$values){
if(!isset($exchange[$key])){
$exchange[$key]=values;
}
}
or
$exchange += $fixed;
or
$exchange = array_merge($fixed,$exchange);
I have an php array like this (var_dump)
I need change one of it's element
array (size=204)
'Address' =>
array (size=3)
'City' =>
array (size=3)
0 => string 'return $this->hasOne(City::className(), ['id' => 'cityId']);'
1 => string 'City' (length=4)
2 => boolean false
'CityDistrict' =>
array (size=3)
0 => string 'return $this->hasOne(CityDistrict::className(), ['id' => 'cityDistrictId']);' (length=76)
1 => string 'CityDistrict' (length=12)
2 => boolean false
'Contacts' =>
array (size=3)
0 => string 'return $this->hasMany(Contact::className(), ['addressId' => 'id']);'
1 => string 'Contact' (length=7)
2 => boolean true
'City' =>
array (size=3)
'Addresses' =>
array (size=3)
0 => string 'return $this->hasMany(Address::className(), ['cityId' => 'id']);'
1 => string 'Address' (length=7)
2 => boolean true
'Region' =>
array (size=3)
0 => string 'return $this->hasOne(Region::className(), ['id' => 'regionId']);' (length=64)
1 => string 'Region' (length=6)
2 => boolean false
'CityDistricts' =>
array (size=3)
0 => string 'return $this->hasMany(CityDistrict::className(), ['cityId' => 'id']);'
1 => string 'CityDistrict' (length=12)
2 => boolean true
'CityDistrict' =>
array (size=2)
Addresses =>
array (size=3)
0 => string 'return $this->hasMany(Address::className(), ['cityDistrictId' => 'id']);'
1 => string 'Address' (length=7)
2 => boolean true
'City' =>
array (size=3)
0 => string 'return $this->hasOne(City::className(), ['id' => 'cityId']);'
1 => string 'City' (length=4)
2 => boolean false
How can i change value 'CityDistrict' in this loop? or 'Addresses'? using php foreach
My code doesn't work please help understand what wrong!
private static function checkExistClass($relations)
{
foreach ($relations as $name => $relation) {
foreach ($relation as $functionName => $functionValue) {
$functionNameGet = 'get' . $functionName;
$directory = new Model;
if (method_exists($directory, $functionNameGet)) {
$relation['funky_key_' . $functionName] = $functionValue;
unset($relation[$functionName]);
}
}
}
return $relations;
}
I interprete your question that you want to rename the array index Addresses to NewAddresses:
$relations['CityDistrict']['NewAddresses'] = $relations['CityDistrict']['Addresses'];
unset($relations['CityDistrict']['Addresses']);
EDIT:
to do that in your foreach loop, change:
$relation['funky_key_' . $functionName] = $functionValue;
unset($relation[$functionName]);
to:
$relations[$name]['funky_key_'.$functionName] = $functionValue;
unset($relations[$name][$functionName]);
maybe this is what you're looking
if (isset($array['n'])) {
$array['name'] = $array['n'];
unset($array['n']);
}
you can see the complete post in Change key in associative array in PHP
see you!
PD Sorry, for my english is not the best
Your loop seems wrong. In the outer loop, $name assumes values such as 'Address', and $relation is an array such as { 'City' => ..., 'CityDistrict' => ... }.
So in the second loop $functionName assumes values such as City, CityDistrict and Contacts.
If you want to change that, you need to do something like #hellcode suggested:
if ('CityDistrict' == $functionName) {
$relations[$name]['NewDistrict'] = $relations[$name][$functionName];
unset($relations[$name][$functionName]);
continue;
}
This looks like a Laravel/Eloquent problem to me. If you can state more precisely what it is that you're trying to accomplish, possibly someone could be of more use.
Also, you seem to want to create a function given its code in a string. To do this you would need create_function (or declare the function as anonymous/lambda function):
$code = "return 42;";
$array['function'] = create_function('', $code);
print $array['function']();
Note that the use of create_function is somewhat deprecated. Also you need a PHP > 5.3+ (or 5.4+ if you go lambda and require $this).
I am trying to parse out certain things within the JSON code, but the problem is that the two groups of arrays that have the information in it I need have random names, here is from the var_dump:
array (size=2)
'results' =>
array (size=1)
0 => string 'Phone.5d5b6fef-a2e0-4b08-cfe3-bc7128b776c3.Durable' (length=50)
'dictionary' =>
array (size=3)
'Person.51f28c76-2993-42d3-8d65-4ea0a66c5e16.Ephemeral' =>
array (size=8)
'id' =>
array (size=5)
...
'type' => null
'names' =>
array (size=1)
...
'age_range' => null
'locations' => null
'phones' =>
array (size=1)
...
'best_name' => string 'John Smith' (length=15)
'best_location' => null
'Location.28dc9041-a0ee-4613-a3b0-65839aa461da.Durable' =>
array (size=30)
'id' =>
array (size=5)
...
'type' => string 'ZipPlus4' (length=8)
'valid_for' => null
'legal_entities_at' => null
'city' => string 'City' (length=8)
'postal_code' => string '12345' (length=5)
'zip4' => string '4812' (length=4)
'state_code' => string 'MO' (length=2)
'country_code' => string 'US' (length=2)
'address' => string 'Main St, City, MO 12345-4812' (length=33)
'house' => null
No I am trying to get best_name from under the part that starts with Person and address under Location. But when I do:
$string = file_get_contents($url);
$json=json_decode($string,true);
var_dump($json);
echo $json['dictionary']['Person']['best_name'];
I get Undefined index: Person error, because the actual object name for Person is:
Person.51f28c76-2993-42d3-8d65-4ea0a66c5e16.Ephemeral which is different every time I do a search. Is there a way to do this without putting the random generated line in?
Hopefully this makes sense, thanks for the help in advance!
If the Person key always starts with the string "Person", than simply do a foreach and check the key which contains this string.
Like:
foreach ($json['dictionary'] as $key => $value) {
if (preg_match('/^Person/', $key)) {
echo $json['dictionary'][$key]['best_name'];
}
}
Of course this get complicated, if you have multiple keys which start with "Person".
You can do the same with "Location" or any other string you need.
How about something like this ... loop through $json['dictionary']'s index keys to find something that starts with "Person".
$foundIt = false;
foreach (array_keys($json['dictionary']) as $key) {
if (substr($key,0,6) == 'Person') {
$foundIt = $key;
break;
}
}
if ($foundIt) { echo $json['dictionary'][$foundIt]['best_name']; }