i'm doing
$sql = $_SESSION['data']->bySQL("SELECT * FROM worlds WHERE field='shippingLicenseItemName'");
and then printing result(i know it is very simple but i'm learning by myself through this site)
echo nl2br(print_r($sql, true));
and in result i'm getting array like this :
Array
(
[atlantis] => Array
(
[shippingLicenseItemName] => pkg_atlantisXfer
)
[australia] => Array
(
[shippingLicenseItemName] => pkg_australiaXfer
)
[avalon] => Array
(
[shippingLicenseItemName] => pkg_avalonXfer
)
)
but i want the results like :
World : "atlantis" and item : "shippingLicenseItemName" and code is : "pkg_atlantisxfer"
World : "australia" and item : "shippingLicenseItemName" and code is : "pkg_australiaxfer"
Don't know where you found the bySQL property, it seems to be returning the data keyed by a column on its own, making things a tad more complicated.
But i'ts nothing you cannot solve with a couple of loops :
foreach($sql as $o_key=>$o_data) {
foreach($o_data as $i_key=>$i_data) {
print "World $o_key and item : $i_key and code is $i_data <br />"l
}
}
Related
I have a problem with the array PHP function, below is my first sample code:
$country = array(
"Holland" => "David",
"England" => "Holly"
);
print_r ($country);
This is the result Array ( [Holland] => David [England] => Holly )
I want to ask, is possible to make the array data become variables? For second example like below the sample code, I want to store the data in the variable $data the put inside the array.:
$data = '"Holland" => "David","England" => "Holly"';
$country = array($data);
print_r ($country);
But this result is shown me like this: Array ( [0] => "Holland" => "David","England" => "Holly" )
May I know these two conditions why the results are not the same? Actually, I want the two conditions can get the same results, which is Array ( [Holland] => David [England] => Holly ).
Hope someone can guide me on how to solve this problem. Thanks.
You can use the following Code.
<?php
$country = array(
"Holland" => "David",
"England" => "Holly"
);
foreach ($country as $index => $value)
{
$$index = $value;
}
?>
Now, Holland & England are two variables. You can use them using $Holland etc.
A syntax such as $$variable is called Variable Variable. Actually The inner $ resolves the a variable to a string, and the outer one resolves a variable by that string.
So there is this thing called
Destructuring
You can do it something like ["Holland" => $eolland, "England" => $england] = $country;
And now you have your array elements inside the variables.
Go read the article above if you want more information about this because it gets really useful (especially in unit tests usind data provders from my experience).
If you want to extract elements from an associative array such that the keys become variable names and values become the value of that variable you can use extract
extract($country);
To check what changed you can do
print_r(get_defined_vars());
Explanation on why the below does not work
$data = '"Holland" => "David","England" => "Holly"';
When you enclose the above in single quotes ' php would recognise it as a string. And php will parse a string as a string and not as an array.
Do note it is not enough to create a string with the same syntax as the code and expect php to parse it as code. The codes will work if you do this
$data = ["Holland" => "David","England" => "Holly"];
However, now $data itself is an array.
A simple copy of an array can be made by using
$data = $country;
Going to try this again as I think my previous post Inserting complex PHP array directly into mongodb was poorly asked, or didn't provide a tl;dr... I'm having a really hard time finding much documentation on anything past the basics.
My array looks like:
[publication] => Array
(
[_id] => 100000009
[title] => title
[author] => author
)
I'm using PHPdriver for mongodb and I directly add the array like this:
$result = $publications->insertOne([
$publication
]);
and while this works, it give me this:
{
"_id" : ObjectId("5a910438834a9c2314006cf6"),
"0" : {
"_id" : "100000009",
"title" : "uf",
"author" : "author"
}
}
How do I make it like this instead:
{
"_id" : "100000009",
"title" : "uf",
"author" : "author"
}
Without manually breaking each line out into a "id" => $publication['id'] inside the insertOne statement?
It seems you want to insert a document, not a mongodb array.
You are creating a new array wrapping your document.
Try this:
$result = $publications->insertOne($publication);
I am struggling with what would appear to be a pretty straight forward task. I have looked at and tried all kinds of functions and suggestion on SO hoping that maybe there is something simple and functional out there. Nothing I tried gives me the logic to do the restructuring.
I have a long complex array. However very much simplified the logic problem I am trying to solve generically is as follows:
$cost_type = Array
(
0 => "ISP2",
1 => "ISP3",
2 => "ISP4"
);
$supplier_name = Array
(
0 => "NAME-A",
1 => "NAME-B",
2 => "NAME-C"
);
$propertyid = Array
(
0 => "property1",
1 => "property2",
2 => "property2"
);
and I need to convert it to the following set of arrays (noting the concatenation of the two arrays with a common property id.....
$property1
(
array['charges']
[0] =>IPS2
array ['names']
[0] =>NAME-A
)
$property2
(
array['charges']
[0] ->IPS3
[1] =>IPS4
array['names']
[0] =>NAME-B
[1] =>NAME-c
)
I have tried everything over the course of the last few hours and a simple solution totally evades me.
If you can join the three arrays as you say in comments above this code will generate the look you want.
I loop through the array with property and keep key as the key to find names and charges in the other subarrays.
$cost_type = Array
(
0 => "ISP2",
1 => "ISP3",
2 => "ISP4"
);
$supplier_name =Array
(
0 => "NAME-A",
1 => "NAME-B",
2 => "NAME-C"
);
$propertyid = Array
(
0 => "property1",
1 => "property2",
2 => "property2"
);
$arr[] = $cost_type;
$arr[] = $supplier_name;
$arr[] = $propertyid;
$result = array();
Foreach($arr[2] as $key => $prop){
$result[$prop]["charges"][] =$arr[0][$key];
$result[$prop]["names"][] = $arr[1][$key];
}
Var_dump($result);
https://3v4l.org/EilvE
The following code converts the original array in the expected result:
$res = array();
foreach($arr[2] as $k => $foo){ // foreach property
if(!isset($res[$foo])){ // add property if not yet in list
$res[$foo] = array(
'charges' => array($arr[0][$k]),
'names' => array($arr[1][$k])
);
}else{ // add new value to already existing property
$res[$foo]['charges'][] = $arr[0][$k];
$res[$foo]['names'][] = $arr[1][$k];
}
}
Check it out here: https://eval.in/904473
Of course, it assumes a bunch on things about the data, but it should work for any number of items.
And if you need the property in another variable, just access it with $res['name of it].
Run this code you will get smiler result as you want :
$twodimantion=array();
$properties=array('property1','property2','property3');
$charges=array('ISP2','ISP3','ISP4');
$names=array('NAME-A','NAME-B','NAME-C');
foreach ($properties as $key => $property) {
$twodimantion['charge'][$key]=$charges[$key];
$twodimantion['names'][$key]=$names[$key];
$twoarray[$property]=$twodimantion;
}
echo '<pre>';
print_r($twoarray);
echo '</pre>';
I can't say I completely follow what you are trying to do, but I think this may be part of the way there for you.
When trying to restructure data in PHP, it's often helpful to create a empty array (or other data structure) to store the new data in first. Then you find a way to loop over your initial data structure that allows you to insert items into your reformatted structure in the right sequence.
<?php
$properties = []; // Array to hold final result
// Loop over your initial inputs
foreach ($groupsOfValues as $groupName => $groupValues) {
$temp = []; // Array to hold each groupings reformatted data
// Loop over the items in one of the inputs
for ($i=0; $i<count($group) && $i<count($properties)+1; $i++) {
if (!is_array($temp[$groupName])) {
$temp[$groupName] = [];
}
$temp[$groupName][] = $group[$i];
}
$properties[] = $temp;
}
First of all, I am VERY new to Mongodb, so please bear with me. I have a mongodb collection that has data like below (in PHP array representation):
Array
(
[_id] => MongoId Object
(
[$id] => 8974439e66777114648b47dc
)
[mechFamily] => X07_B22
[mechName] => X07_B22_61
[mechType] => Original
[spans] => Array
(
[noOfSpans] => 5
[span] => Array
(
[0] => Array
(
[type] => solid
[clipShape] => rectangle
[id] => 2d8c1d2a6756323beca8e6e59823e3b
My requirement is that I need to pass condition to find() which will give me only those records where noOfSpansis 10.
I tried:
$myCollection->find(array("spans.noOfSpans" => 10));
But that does not return anything, I looked at the nest query sections in docs as well as SO, but the answers are pretty confusing. Can anyone tell me how to query this mongodb structure?
I'm not fluent in PHP's syntax, so I'm going to translate your document into mongo shell syntax and then propose a query. Let me know if I did it wrong and I will try to correct it.
{
"_id" : "8974439e66777114648b47dc",
"mechFamily" : "X07_B22",
"mechName" : "X07_B22_61",
"mechType" : "Original",
"spans" : {
"noOfSpans" : 5,
"span" : [
{
"type" : "solid",
"clipShape" : "rectangle",
"id" : "2d8c1d2a6756323beca8e6e59823e3b"
}
]
}
}
The query to match documents where spans.noOfSpans is 10 is
db.collection.find({ "spans.noOfSpans" : 10 })
which looks to be the same as your PHP query that you say isn't working. Is my document structure incorrect? What do you mean when you say the find isn't returning anything? It's returning a cursor with no results? How are you checking?
I would like to do that :
$product[] = Array (
"article_title" => valeures,
"article_id" => ,
"article_value" => ,
"article_price" =>
"article_picture1" => ,
"article_picture2" => ,
"article_picture3" => ,
"article_picture4" => ,
"article_friends[]" => array ( "name" => ,
),
);
But I know it won't work, I've been looking for hours on the net and on php.net, but I don't know how to do.
What I want to do, is getting a product[] array that can handle product[1]; product [2] ... and inside it, I got the same process : the "article_firends[]" will be autoincremented to do something like that :
In product[1] : artcile_friends[1][name] = John ; article_freidns[2][name] = Nina ... and so on with product[2] ...
This has been implementedin a foreach loop so it Should manage the keys automatically ..
How can I build it ?
Many Thanks,
Miles
"article_friends[]" => array ( "name" => ,
doesn't create a new article_friends sub array. It creates a key in the parent $product array whose name happens to be article_friends[].
Remove the quotes from around the key, so you end up with
article_friends[] => array ( "name" => , ...
This totally does work.
You're getting an error because of valeures, either this is a variable, than add a '$' or it's a string, than add quotes.
"article_friends[]" => array ( "name" => , is wrong too, you have to add a value after "name".
The trailing comma in the 'article_friends[]' line should be omitted to.