I have a variable/data structure in its original state, that when var_dump() 'ed', looks like this:
string 'some name' (length=13)
string 'another name ' (length=16)
string 'another name ' (length=11)
I have a function that gets the same names and pre-pends a letter to them. The results are stored in an array like this:
array (size=3)
1 =>
array (size=1)
'name' => string 'A - the name ' (length=20)
2 =>
array (size=1)
'name' => string 'A - the name ' (length=15)
3 =>
array (size=1)
'name' => string 'C - the name' (length=17)
I need the modified data in the same state as the original.
How do I convert the array structure to look like the original?
Thank you.
I understand you wish to remove the letter?
foreach($myarrayname as $key => &$value) {
$value = substr($value, 4);
}
To get a single element, use
$myarrayname[0] // for the first one, ...[1] for the second one and so on
Related
I can easly explore json for example:
foreach($json_a['somwhere'][1]['somwhere_deeper'] as $something){
var_dump($something);
}
This code makes me print something like this:
C:\wamp64\www\dothejob.php:7:
array (size=2)
'name' => string 'John' (length=17)
'value' => string '15' (length=4)
C:\wamp64\www\dothejob.php:7:
array (size=2)
'name' => string 'Joanna' (length=6)
'value' => string '23' (length=2)
C:\wamp64\www\dothejob.php:7:
array (size=2)
'name' => string 'John' (length=17)
'value' => string '55' (length=10)
C:\wamp64\www\dothejob.php:7:
array (size=2)
'name' => string 'Joanna' (length=11)
'value' => string '55' (length=5)
So I'm sure I'm in a right place, but now the question is how to print only value which is in array, where name is Joanna?
I know it should be easy If statement, but I'm not sure how those keys/values works, Its easy question, but I'm beginner with php... :) ps. I was looking for help but didn't found solution yet.
Can't use $something[n], because they are not allways on the same "place", so only right solution is something like this:
I'm looking for something like this:
if 'name' is 'Joanna':
print value of 'value'
You can use $something[n] because you have an associative array :
foreach($json_a['somwhere'][1]['somwhere_deeper'] as $something){
if ($something['name'] == 'Joanna') {
var_dump($something);
}
}
Output should be :
C:\wamp64\www\dothejob.php:7:
array (size=2)
'name' => string 'Joanna' (length=6)
'value' => string '23' (length=2)
C:\wamp64\www\dothejob.php:7:
array (size=2)
'name' => string 'Joanna' (length=11)
'value' => string '55' (length=5)
Of course, if you want to var_dump the value only, use var_dump($something['value']).
you need to update naming convension of Variable
foreach($json_a['somwhere'][1]['somwhere_deeper'] as $key => $value){
echo $key." : ".$value
}
Output of above code will be something like
John : 15
Johnna : 23
foreach(array as key => value)
{
//key represent array key
//value represent value of that array
}
Let me know in case of any concern.
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';
}
}
}
I've got a form which users can add new rows by clicking on a button and automatically, it adds +1 on the field's name.
So for example, I've got my train_id_1, train_type_1 and my user wants to add a new one, so now I've got train_id_2 and train_type_2.
In order to save this in my database, I would like to sort and seperate train_type_1 / train_type_2... to make a foreach and then to save in my database.
So, the var_dump of my $_POST looks like :
array (size=60)
'train_id_1' => string ' 07:36' (length=6)
'train_type_1' => string ' -Z' (length=3)
'user_id_1' => string 'CPN' (length=3)
'event_criter_1' =>
array (size=3)
0 => string 'test' (length=4)
1 => string '234' (length=3)
2 => string '532' (length=3)
'train_id_2' => string ' 08:32' (length=6)
'train_type_2' => string ' -X' (length=3)
'user_id_2' => string 'CPN' (length=3)
'event_criter_2' =>
array (size=3)
0 => string 'TESTG' (length=5)
1 => string 'GGG' (length=3)
2 => string 'AETG' (length=4)
'train_id_3' => string ' 08:36' (length=6)
'train_type_3' => string ' -Z' (length=3)
'user_id_3' => string 'CPN' (length=3)
'event_criter_3' =>
array (size=1)
0 => string '' (length=0)
'train_id_4' => string ' 09:04' (length=6)
'train_type_4' => string ' -X' (length=3)
'user_id_4' => string 'CPN' (length=3)
'event_criter_4' =>
array (size=1)
0 => string '' (length=0)
Do you know how I can make abcd_1 separate from abcd_2 to make my foreach (or another solution) ?
Thank you!
Loop through your array, matching _1 and insert the posted elements as one row. Then increment and match _2 etc..
$postedElements = $_POST;
$elementsPerRow = 4;
$numRows = count($postedElements)/$elementsPerRow;
// loop through the number of 'rows' to insert
for($i=1;$i<=$numRows;$i++){
// Build an array to store matched elements to insert
$elementsToInsert = array();
//process the complete _POST array each time...
foreach($postedElements as $name => $value){
// ...get the 'row' (the bit after the underscore)...
//list($value,$row) = explode('_',$name); // doesn't work for 2 underscores..
$row = end(explode($name)); // This will
if($row == $i){
// ...and add elements that match to an insert array
// $elementsToInsert[] = $name;
$elementsToInsert[$name] = $value;
}
}
// insert $elementsToInsert into DB
}
I think, at first You need to have a amount of records in array through count($arr). Then use usual for loop:
//output of field names
for ($i = 0; $i < count($arr); $i++){
//Work with array
}
Make a new array.
Then add a data from loop into new array.
I think it could help You.
And by which reason You need to sort them?
I want have an array that is created from a database to populate a table
array (size=2)
number of friends => 3
friends details => array (size=3)
0 =>
array (size=4)
'bithday' => string '2000-02-04' (length=10)
'email' => string 'someone#example.com' (length=11)
'town' => string 'OXFORD' (length=6)
'status' => string 'FRIENDS' (length=5)
2 =>
array (size=4)
'bithday' => string '2000-02-04'(length=10)
'email' => string 'someone#example.com' (length=11)
'town' => string 'OXFORD' (length=6)
'status' => string 'NOT FRIENDS' (length=9)
3 =>
array (size=4)
'bithday' => string '2000-02-04'(length=10)
'email' => string 'someone#example.com' (length=11)
'town' => string 'CAMBRIDGE' (length=8)
'status' => string 'FRIENDS' (length=5)
I have (unsuccessfully) being trying to do a str_replace on the email string if the correct conditions are met, so I want to be able to replace the email address of all my friends in oxford with a button that allows me to email them with one press
so someone#example.com becomes
<button action=invitePal>Invite your pal!</button>
But I only want this to happen if the array says that they meet both criteria to change it, so if I am having a party in Oxford, the invite button only appears on the entries for 'friends' in Oxford.
A pretty standard method for this would be to loop though like so:
foreach($people as $index => $person) {
if($person['status'] === 'FRIENDS' && $person['town'] === 'OXFORD') {
// Change the original string, echo it out, or do whatever.
// If you need the email in this string, you can reference it via:
// $person['email']
$people[$index]['email'] = '<button action=invitePal>Invite your pal!</button>';
}
}
An alternative method would be to use array_map and an anonymous function.
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']; }