I am using the PHP Simple HTML DOM Parser to scrape some results from a page.
At the moment I am having a problem with the function as it is not returning the array "$result".
Any help would be greatly appreciated :)
The result of the array:
array(1) { [0]=> array(6) { ["itemid"]=> string(6) "123456" ["title"]=> string(21) "XXX Prod1" ["unit"]=> string(6) "500ml " ["price"]=> string(4) "2.59" } [1]=> array(6) { ["itemid"]=> string(6) "123457" ["title"]=> string(27) "XXX Prod2" ["unit"]=> string(6) "500ml " ["price"]=> string(5) "10.49" }
Code in question:
function parseItems($html) {
foreach($html->find('div.product-stamp-inner') as $content) { //Finds each individual product on page and extracts its details and stores it into its own array
$detail['itemid'] = filter_var($content->find('a.product-title-link', 0)->href, FILTER_SANITIZE_NUMBER_FLOAT);
$detail['title'] = $content->find('span.title', 0)->plaintext;
$detail['unit'] = $content->find('span.unit-size', 0)->plaintext;
$detail['price'] = filter_var($content->find('span.price', 0)->plaintext, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND);
$result[] = $detail; //Puts all individual product arrays into one large array
}
//var_dump($result); --Testing purposes
return $result;
}
I guess what you have a piece of code like so
parseItems($html);
When it should be the following because it is returning a variable and needs a variable to hold its returning result
$retval = parseItems($html);
Related
need some help with splitting mysql single column query array into different php variables here.
example:
here's the query, it's pretty simple to be honest.
but, i'm running out of ideas right now.
$string = "select Description from tblQuestion
where Employeeid = '$param'"
$query = $this->db->query($string);
$result = return $query->result_array();
btw, i am using Codeigniter and i tried to var_dump and the results are like this.
array(9) { [0]=> array(1) { ["Description"]=> string(5) "tidak" } [1]=> array(1) { ["Description"]=> string(5) "tidak" } [2]=> array(1) { ["Description"]=> string(5) "tidak" } [3]=> array(1) { ["Description"]=> string(5) "tidak" } [4]=> array(1) { ["Description"]=> string(5) "tidak" } [5]=> array(1) { ["Description"]=> string(5) "tidak" } [6]=> array(1) { ["Description"]=> string(5) "tidak" } [7]=> array(1) { ["Description"]=> string(5) "tidak" } [8]=> array(1) { ["Description"]=> string(5) "tidak" } }
i tried to use json_encode and the result is
[{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"}]
the question is.
how do i convert this stack of arrays into different variables like this?
$var0 = "tidak";
$var1 = "tidak";
$var2 = "tidak";
$var3 = "tidak";
and on and on....
thanks in advance.
cheers!
Put the results in a foreach loop and assign the values to a dynamic variable...
sample code like,
foreach($results as $key=>$val){
$str = 'var'.$key;
$$str = $val['Description'];
}
echo $var0;
I've been trying to create an array from a mysqli query that creates a new class 'Book'. When I run this code, it returns the correct number of rows that I have in my database, but it won't display any of the values in the rows. Definitely a newb to a lot of this stuff, any help is really appreciated! Here's the code I'm running. If I just put in a pre-populated array, the code all works great. My problem is getting the values from the database into this array.
$db = new db();
$conn = $db->connect();
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);
$thearray = array();
while($array_ar = mysqli_fetch_array($result)){
$thearray[] = array(
$array_ar['username'] => new Book($array_ar['username'], $array_ar['first_name'], $array_ar['last_name']),
);
}
echo 'Rows found: ' . mysqli_num_rows($result);
return $thearray;
Ok, so I var_dump($thearray); and it gives me a multidimensional array, which is why it's returning empty values. I think I need it to return an associative array? Here's what it var_dump($thearray); returns:
array(4) {
[0]=> array(1) {
["joshua"]=> object(Book)#6 (3) {
["title"]=> string(6) "joshua" ["author"]=> string(6) "Joshua" ["description"]=> string(9) "Lundquist"
}
}
[1]=> array(1) {
["matthew"]=> object(Book)#7 (3) {
["title"]=> string(7) "matthew" ["author"]=> string(4) "Matt" ["description"]=> string(3) "Alm"
}
}
[2]=> array(1) {
["roger"]=> object(Book)#8 (3) {
["title"]=> string(5) "roger" ["author"]=> string(5) "Roger" ["description"]=> string(9) "Lundquist"
}
}
[3]=> array(1) {
["norm"]=> object(Book)#9 (3) {
["title"]=> string(4) "norm" ["author"]=> string(4) "Norm" ["description"]=> string(5) "Shupe"
}
}
}
Next I thought I would take a look at what the var_dump(); for my hard-coded array looks like, and it output this (this one works):
array(3) {
["Jungle Book"]=> object(Book)#3 (3) {
["title"]=> string(11) "Jungle Book" ["author"]=> string(10) "R. Kipling" ["description"]=> string(15) "A classic book."
}
["Moonwalker"]=> object(Book)#4 (3) {
["title"]=> string(10) "Moonwalker" ["author"]=> string(9) "J. Walker" ["description"]=> string(7) "another"
}
["PHP for Dummies"]=> object(Book)#5 (3) {
["title"]=> string(15) "PHP for Dummies" ["author"]=> string(14) "Some Smart Guy" ["description"]=> string(18) "and the final one."
}
}
It makes sense to me what's happening, but I'm not sure how to fix my code. Thank you for the help!
Don't Panic's answer got me looking in the right direction. I needed to create an associative array, not a multidimensional array. At first I was accidentally creating the multidimensional array by setting $thearray equal to "array();". By removing that, it filled in the values correctly. Thanks again for the help!
$db = new db();
$conn = $db->connect();
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);
$thearray = array();
while($array_ar = mysqli_fetch_array($result)){
$title = $array_ar['username'];
$author = $array_ar['username'];
$description = $array_ar['username'];
$thearray[] = $title = new Book($title, $author, $description);
}
var_dump($thearray);
echo 'Rows found: ' . mysqli_num_rows($result);
return $thearray;
I think this is the problem.
$thearray[] = array(
$array_ar['username'] => new Book(
$array_ar['username'], $array_ar['first_name'], $array_ar['last_name']),
);
It looks like the differences between what you're getting and what you want is 1) You're wrapping the input to $thearray in an extra array() layer, and 2) You won't get string keys like you have in your hard-coded array if you just add the items using [].
Try it like this instead:
// use the username as a key, since that is what it looks like you're using as the 'title'
$thearray[$array_ar['username']] = new Book(
$array_ar['username'], $array_ar['first_name'], $array_ar['last_name']);
I need to get the objects information for "label", "name" where value=true in a PHP variable and not were value=false.
How is this done with this JSON array?
If I make a var_dump of the JSON I get this:
array(8) {
[0]=>
object(stdClass)#8 (3) {
["label"]=>
string(4) "Name"
["name"]=>
string(7) "txtName"
["value"]=>
bool(true)
}
[1]=>
object(stdClass)#9 (3) {
["label"]=>
string(6) "E-mail"
["name"]=>
string(8) "txtEmail"
["value"]=>
bool(true)
}
[2]=>
object(stdClass)#10 (3) {
["label"]=>
string(12) "Phone Number"
["name"]=>
string(8) "txtPhone"
["value"]=>
bool(false)
}
[3]=>
object(stdClass)#11 (3) {
["label"]=>
string(19) "Mobile Phone Number"
["name"]=>
string(14) "txtMobilePhone"
["value"]=>
bool(false)
}
}
$arr = array();
$i = 0;
foreach($json as $key => $items) {
if($items->value == true) {
$arr[$i]['label'] = $items->label;
$arr[$i]['name'] = $items->name;
$i++;
}
}
You can decode it as an object or an array, in this example I use an array.
First you want to take the JSON encoded information and decode it into a PHP array, you can use json_decode() for this:
$data = json_decode($thejson,true);
//the Boolean argument is to have the function return an array rather than an object
Then you can loop through it as you would a normal array, and build a new array containing only elements where 'value' matches your needs:
foreach($data as $item) {
if($item['value'] == true) {
$result[] = $item;
}
}
You then have the array
$result
at your disposal.
Simplification of the suggestions proposed by users JohnnyFaldo and som:
$data = json_decode($thejson, true);
$result = array_filter($data, function($row) {
return $row['value'] == true;
});
I am parsing a JSON being posted from another website and one of the nodes has a child.
$inputJSON = file_get_contents('php://input');
$input= json_decode( $inputJSON); //convert JSON into object
$order_number = $input->{'order_no'};
$name = $input->{'name'};
$street_address = $input->{'address_1'};
$city =$input->{'city'};
$state = $input->{'region'} ;
$zip = $input->{'postal_code'};
I am being able to read all the values. However, the product section has the format
<items>
<product_code></product_code>
<product_name></product_name>
</items>
I am trying to read it as
$product_id = $input->{'items'}{'product_code'};
$product_description = $input->{'items'}{'product_name'};
But I am getting no data in my variables. What is the correct syntax?
Thanks.
Edit: The JSON output
object(stdClass)#1 (20) {
["test_order"]=>
string(1) "Y"
["shop_no"]=>
string(6) "142319"
["order_no"]=>
string(12) "TU5495467701"
string(5) "Smith"
["city"]=>
string(5) "Bosei"
["postal_code"]=>
string(6) "123456"
["order_total"]=>
string(5) "39.00"
["country"]=>
string(2) "HK"
["telephone"]=>
string(8) "12345678"
["pay_source"]=>
string(2) "CC"
["base_currency"]=>
string(3) "USD"
["items"]=>
array(1) {
[0]=>
object(stdClass)#2 (9) {
["product_price"]=>
string(5) "39.00"
["product_name"]=>
string(12) "Abcd Product"
["product_code"]=>
string(8) "142319-1"
}
}
["first_name"]=>
string(4) "John"
}
As you wrote:
["items"]=>
array(1) {
[0]=>
object(stdClass)#2 (9) {
["product_price"]=>
string(5) "39.00"
["product_name"]=>
string(12) "Abcd Product"
["product_code"]=>
string(8) "142319-1"
}
}
The items element is an array, contains multiple objects, so you must use this syntax:
$product_id = $input->items[0]->product_code;
$product_description = $input->items[0]->product_name;
And, If the items are more than one, you should use a loop:
for ($i = 0; $i < count($input->items); $i++) {
$product_id = $input->items[$i]->product_code;
$product_description = $input->items[$i]->product_name;
}
$product_id = $input->items[0]->product_code;
More likely though you will want to loop through $input->items instead of directly accessing just the first index.
I'm not shure but I think you can use it as an array and do something like this:
$product_id = $input['items']['product_code'];
$product_description = $input['items']['product_name'];
In php json_decode returns an array. So you should be able to access it simply as you access array values.
$product_id = $input['items']['product_code'];
I'm receiving a JSON and trying to interpret some values using PHP.
Example snippet from a JSON dump:
["11811"]=>
object(stdClass)#15 (11) {
["parent_area"]=>
NULL
["generation_high"]=>
int(19)
["all_names"]=>
object(stdClass)#16 (0) {
}
["id"]=>
int(11811)
["codes"]=>
object(stdClass)#17 (3) {
["ons"]=>
string(2) "08"
["gss"]=>
string(9) "E15000008"
["unit_id"]=>
string(5) "41421"
}
["name"]=>
string(10) "South East"
["country"]=>
string(1) "E"
["type_name"]=>
string(15) "European region"
["generation_low"]=>
int(1)
["country_name"]=>
string(7) "England"
["type"]=>
string(3) "EUR"
}
As there is lots of (nested) data, I need to obtain the value of ["name"] where ["type_name"] == 'European region'.
Thanks.
You could use array_filter()
$data_array = array(...);
function is_european($data) {
return $data->type_name == 'European region';
}
$filtered = array_filter($data_array,'is_european');
And then use filtered array to obtain values.
Maybe a better way would be to use JsonPath, like this, assuming your array is a result of decoding JSON (object):
$names = jsonPath($data_object, "$.[?(#['type_name'] == 'European region')].name");
Haven't tried this myself, it may need a bit of correction.
Try this:
<?php
$json = JSON_decode(str,true);
$arr = Array();
foreach($json as $f) {
/* eg. $f = $json["11811"] */
if($f['type_name'] == 'European region') {
$arr[] = $f['name'];
}
}
?>