Add array data by name php - php

I'm new to arrays. I'd like to know if you can add data to the array by name.
This would be an example of what I want to achieve
example:
$guitars = ['names',['Warvick', 'Gibson', 'Fender']];
$guitars[names][] = "Ibanez";
echo "<pre>";
var_dump($guitars);
echo "</pre>";
result
WARNING Use of undefined constant names - assumed 'names' (this will throw an Error in a future version of PHP) on line number 3
array(3) {
[0]=>
string(5) "names"
[1]=>
array(3) {
[0]=>
string(7) "Warvick"
[1]=>
string(6) "Gibson"
[2]=>
string(6) "Fender"
}
["names"]=>
array(1) {
[0]=>
string(6) "Ibanez"
}
what I want is to add the data "ibanez" to the array "names" but nevertheless create a new one.
I need it to stay this way. Is there any way to access the data directly by the name "names"?
array(2) {
[0]=>
string(5) "names"
[1]=>
array(4) {
[0]=>
string(7) "Warvick"
[1]=>
string(6) "Gibson"
[2]=>
string(6) "Fender"
[3]=>
string(6) "Ibanez"
}
}

You want to use associative arrays. In this case, for example, use the arrow token to associate the guitar names with the "names" key.
$guitars = ['names' => ['Warvick', 'Gibson', 'Fender']];
$guitars['names'][] = "Ibanez";
echo "<pre>";
var_dump($guitars);
echo "</pre>";

Related

Merging multi dim arrays on ID in array value

I have a bigger multi dim array I pull from a db.
array(43593) {
[0]=>
array(4) {
["artnum"]=>
string(5) "0422272221"
["ekprice"]=>
string(5) "52.78"
["ekpriceist"]=>
string(5) "52.78"
["lieferantnr"]=>
string(7) "7000202"
}
[1]=>
array(4) {
["artnum"]=>
string(5) "04233337133333"
["ekprice"]=>
string(5) "49.45"
["ekpriceist"]=>
string(5) "49.45"
["lieferantnr"]=>
string(7) "7000211"
}
from another DB server I pull this array
array(73287) {
[0]=>
array(2) {
["ARTNUM"]=>
string(5) "0422272221"
["QUALITAETSMERKMAL"]=>
string(1) "p"
}
[1]=>
array(2) {
["ARTNUM"]=>
string(5) "04233337133333"
["QUALITAETSMERKMAL"]=>
string(1) "m"
}
When I excecute the following from user interface
foreach($aStockArticles as $aStockArticle) {
foreach($aArticleStamm as $artikel)
{
if($aStockArticle['artnum'] == $artikel['artnum'])
{
$aStockArticle['quality'] = $artikel['QUALITAETSMERKMAL'];
}
} }
I recieve
[line 451] [code ] [message Maximum execution time of 30 seconds
exceeded]
As you can see, the amount of articles and the sequence they are ordered can differ from each other.
Since I have no access environment variables
I usually would do a simple join in the db query but in this case its not possible due to the two db servers.
QUESTION:
How do I merge these arrays on the ID (artnum)?
So it looks like the following and stay within the excecution time?
array(43593) {
[0]=>
array(4) {
["artnum"]=>
string(5) "0422272221"
["ekprice"]=>
string(5) "52.78"
["ekpriceist"]=>
string(5) "52.78"
["lieferantnr"]=>
string(7) "7000202"
["QUALITAETSMERKMAL"]=>
string(1) "p"
}
[1]=>
array(4) {
["artnum"]=>
string(5) "04233337133333"
["ekprice"]=>
string(5) "49.45"
["ekpriceist"]=>
string(5) "49.45"
["lieferantnr"]=>
string(7) "7000211"
["QUALITAETSMERKMAL"]=>
string(1) "m"
}

How to get last value from this array?

How to get the last array value of this var_dump?
I do a var_dump on a variable ($submission) and get this:
object(stdClass)#148 (8) {
["sid"]=> string(3) "199"
["nid"]=> string(4) "3042"
["submitted"]=> string(10) "1386113448"
["remote_addr"]=> string(9) "127.0.0.1"
["uid"]=> string(2) "21"
["name"]=> string(8) "SClosson"
["is_draft"]=> string(1) "0"
["data"]=> array(1) {
[1]=> array(1) {
[0]=> string(8) "blahblah"
}
}
}
So I need to store blahblah in a variable from the above array, but how?
Thought I could just get it by doing this: $submission['data'][1][0], but that doesn't work. How do I return blahblah from this?
If you need an array, you can type cast it
$result = (array) $submission;
Or as an object, access the data as public properties
echo $submission->data[1][0];
You can use array_pop if you want to get the last value of an array.
http://www.php.net/manual/en/function.array-pop.php

php code - trying to loop array

I am noticing some strange behavior while looping through some data. I'm sure it's something simple, but I can't seem to be able to find the bug.
I have the following logic:
<?php
print 'dumping data : <BR>';
var_dump($portvlan);
print '<BR>';
print 'looping through data: <BR>';
foreach ($portvlan as $vlandetail){
echo 'Vlanid: '.$vlandetail['VlanId'].'<BR>';
echo 'Name: '.$vlandetail['Name'].'<BR>';
echo 'Mode: '.$vlandetail['Mode'].'<BR>';
}
?>
This is the output that I'm getting:
dumping data :
array(3) { ["VlanId"]=> string(2) "33" ["Name"]=> string(6) "USR_33" ["Mode"]=> string(6) "Access" }
looping through data:
Vlanid: 3
Name: 3
Mode: 3
Vlanid: U
Name: U
Mode: U
Vlanid: A
Name: A
Mode: A
What I was expecting was to see it print a row with 3 cells, with the following values:
33, USR_33, Access.
Can you tell me where I'm going wrong?
Thanks.
EDIT 1
This logic works fine when the $portvlan array has more than one entry.
For example, on another set of data, the var_dump gives this result:
array(6) { [0]=> array(3) { ["VlanId"]=> string(1) "1" ["Name"]=> string(1) "1" ["Mode"]=> string(5) "Trunk" } [1]=> array(3) { ["VlanId"]=> int(2) ["Name"]=> int(2) ["Mode"]=> string(5) "Trunk" } [2]=> array(3) { ["VlanId"]=> int(3) ["Name"]=> int(3) ["Mode"]=> string(5) "Trunk" } [3]=> array(3) { ["VlanId"]=> int(4) ["Name"]=> int(4) ["Mode"]=> string(5) "Trunk" } [4]=> array(3) { ["VlanId"]=> int(5) ["Name"]=> int(5) ["Mode"]=> string(5) "Trunk" } [5]=> array(3) { ["VlanId"]=> string(2) "33" ["Name"]=> string(2) "33" ["Mode"]=> string(5) "Trunk" } }
And the loop logic works fine.
$vlandetail will be populated with a different item from the array on every iteration of the loop. You shouldn't be treating $vlandetail as an array. Just use it directly.
To get the key name of the array entry, you have to change the loop structure to this:
foreach ($portvlan as $key => $vlandetail) {
echo $key . ': ' . $vlandetail . '<br>';
}
How you get your $portvlan?
If you can know is $portvlan is not an array of $vlandetail, you can simply do this,
$portvlan = array($portvlan);
// start loop
Or you can do this before you loop it, to make sure $portvlan is a numerical array formed by $vlandetail
$portvlan = (isset($portvlan[0]))?$portvlan:array($portvlan);
// start loop

How to sort php array by value and add an array to another

Okay, so I'm writing an app that allows me to see steam data from a database of whoever registered.
I met a problem. Firstly, the steam API for multiple users is not standardized. (e.g. everytime you refresh this, the position of user changes (What kind of API does this?!)
Since steam does not standardize the API, I'll have to do it myself, so after doing a json_decode($url, true). It is not an assoc array.
I want to sort the assoc array by the steam ID (which is numeral) and match them against my own database of user (also contains steam ID, but can be sorted in the database), so how do I go about doing that?
E.g.
Array 1:
array(3) {
[0]=>
array(2) {
["steam_id32"]=>
string(17) "76561198025035234"
["name"]=>
string(7) "Mitsuki"
}
[1]=>
array(2) {
["steam_id32"]=>
string(17) "76561197968270056"
["name"]=>
string(3) "nrn"
}
[2]=>
array(2) {
["steam_id32"]=>
string(17) "76561197982490298"
["name"]=>
string(4) "Ximp"
}
}
Array 2:
array(1) {
["response"]=>
array(1) {
["players"]=>
array(3) {
[0]=>
array(16) {
["steamid"]=>
string(17) "76561197982490298"
["communityvisibilitystate"]=>
int(3)
["profilestate"]=>
int(1)
["personaname"]=>
string(53) "……‮‮‮‮‮‮‮‮‮‮Ximp ……FUS RO DAH"
["lastlogoff"]=>
int(1328569605)
["profileurl"]=>
string(34) "http://steamcommunity.com/id/ximp/"
["avatar"]=>
string(114) "http://media.steampowered.com/steamcommunity/public/images/avatars/f8/f8ee0cf00a2ec20417bf5b26b99fd6fb4dc176c1.jpg"
["avatarmedium"]=>
string(121) "http://media.steampowered.com/steamcommunity/public/images/avatars/f8/f8ee0cf00a2ec20417bf5b26b99fd6fb4dc176c1_medium.jpg"
["avatarfull"]=>
string(119) "http://media.steampowered.com/steamcommunity/public/images/avatars/f8/f8ee0cf00a2ec20417bf5b26b99fd6fb4dc176c1_full.jpg"
["personastate"]=>
int(1)
["realname"]=>
string(9) "I life in"
["primaryclanid"]=>
string(18) "103582791430354400"
["timecreated"]=>
int(1146939839)
["gameextrainfo"]=>
string(20) "The Binding Of Isaac"
["gameid"]=>
string(6) "113200"
["loccountrycode"]=>
string(2) "DE"
}
[1]=>
array(14) {
["steamid"]=>
string(17) "76561197968270056"
["communityvisibilitystate"]=>
int(3)
["profilestate"]=>
int(1)
["personaname"]=>
string(3) "nrn"
["lastlogoff"]=>
int(1328618220)
["profileurl"]=>
string(34) "http://steamcommunity.com/id/nrnx/"
["avatar"]=>
string(114) "http://media.steampowered.com/steamcommunity/public/images/avatars/50/50b908e0aa2c730fa0f68ab0afc8b04fddb133f1.jpg"
["avatarmedium"]=>
string(121) "http://media.steampowered.com/steamcommunity/public/images/avatars/50/50b908e0aa2c730fa0f68ab0afc8b04fddb133f1_medium.jpg"
["avatarfull"]=>
string(119) "http://media.steampowered.com/steamcommunity/public/images/avatars/50/50b908e0aa2c730fa0f68ab0afc8b04fddb133f1_full.jpg"
["personastate"]=>
int(1)
["realname"]=>
string(9) "Nathaniel"
["primaryclanid"]=>
string(18) "103582791432850562"
["timecreated"]=>
int(1092771678)
["loccountrycode"]=>
string(2) "US"
}
[2]=>
array(14) {
["steamid"]=>
string(17) "76561198025035234"
["communityvisibilitystate"]=>
int(3)
["profilestate"]=>
int(1)
["personaname"]=>
string(23) "[ProudiA] Mitsuki Sakai"
["lastlogoff"]=>
int(1328621807)
["commentpermission"]=>
int(1)
["profileurl"]=>
string(42) "http://steamcommunity.com/id/mitsukisakai/"
["avatar"]=>
string(114) "http://media.steampowered.com/steamcommunity/public/images/avatars/9d/9d279f349422cbbed55adf1c8eabb0924ea0a719.jpg"
["avatarmedium"]=>
string(121) "http://media.steampowered.com/steamcommunity/public/images/avatars/9d/9d279f349422cbbed55adf1c8eabb0924ea0a719_medium.jpg"
["avatarfull"]=>
string(119) "http://media.steampowered.com/steamcommunity/public/images/avatars/9d/9d279f349422cbbed55adf1c8eabb0924ea0a719_full.jpg"
["personastate"]=>
int(1)
["realname"]=>
string(12) "酒井å‚è¼"
["primaryclanid"]=>
string(18) "103582791432752089"
["timecreated"]=>
int(1273714689)
}
}
}
}
For sortig array you can find a list of all function that you need here
Update:
first you must create a 1d array from a 2d or 3d you can use this code to make an easy access array and sortable (this an example):
<?php
$inArr;//This is the 2D array
$outArr = array();
for($i=0;$i<count($inArr);$i++){
$outArr[$i] = $inArr[$i][0];
?>
then you can sort it with ksort() or krsort() function.and for adding an array to another :
<?php
$stack = array("value1", "value2");
array_push($stack, "value3", "value4");
print_r($stack);
?>

Targeting specific group when creating a campaign with mailChimp API

I cannot seem to figure out how to send to a group in my list using mailChimp API
My code looks like this:
$conditions = array('field'=>'interests-1', 'op'=>'all', 'value'=>'myGroup');
$opts = array('match'=>'any', 'conditions'=>$conditions);
$retval = $api->campaignSegmentTest($listId, $opts);
But this yields bool(false). When fetched by
$retval = $api->listInterestGroupings($listId);
my list looks like this:
array(1) {
[0]=>
array(5) {
["id"]=>
int(1)
["name"]=>
string(10) "myList"
["form_field"]=>
string(5) "radio"
["display_order"]=>
string(1) "0"
["groups"]=>
array(5) {
[0]=>
array(4) {
["bit"]=>
string(1) "1"
["name"]=>
string(9) "myGroup"
["display_order"]=>
string(1) "1"
["subscribers"]=>
int(1)
}
[1]=>
array(4) {
["bit"]=>
string(1) "2"
["name"]=>
string(9) "myGroup_2"
["display_order"]=>
string(1) "2"
["subscribers"]=>
int(1)
}
}
}
}
I have looked in the API documentation and searched for the answer, but cannot seem to figure it out. Grateful for help!
Looks like you are using the PHP wrapper - the first thing to do, like the examples included with it do, is to check for any errors by looking at $api->errorCode before messing with the $retval.
When you do that I'm certain you will see an error telling you that you haven't passed a proper "conditions" parameter since it is an array of arrays, not an array.

Categories