1

So I need to get the part of the array that is holding a specific id, so that I can use the other variables within it.

Here is my current code to create the array:

<?php foreach ($list_subjects as $subject): ?>
    subjects.push({
        id: parseInt(<?php echo $subject['subject_id']; ?>),
        subject_name: "<?php echo $subject['subject_name']; ?>",
        course_type: "<?php echo $subject['course_type']; ?>",
        num_videos: parseInt(<?php echo $subject['num_videos']; ?>),
        subject_tag: "<?php echo $subject['subject_tags']; ?>"
    });
<?php endforeach; ?>

(I know mixing PHP in this fashion isn't good practise, but bear with me for this question)

This is the code I'm using to at least try and check if the value is in the array, but it is returning -1 every time, and the value of course_id IS in the array.

alert($.inArray(course_id, subjects.id));

Why is this happening?

4
  • 2
    jQuery, and all JavaScript (with the exception of node.js) works client-side; this means the PHP is not only 'not good practice' but also entirely irrelevant to the question you're asking. Please could you show the rendered mark-up/JavaScript as seen in the browser (view source)? Commented Jul 5, 2012 at 11:48
  • Hey David, sure can: subjects.push({ id: 64, subject_name: "ABCs", course_type: 1, num_videos: 37, num_badges: 9, num_quizzes: 59, subject_tag: "Subject One" }); Commented Jul 5, 2012 at 11:59
  • Also, what would you recommend the best way of getting php array values in js would be? I know I can make ajax calls etc, but is there a way that I can get them on page load, without having to make an additional request? Commented Jul 5, 2012 at 12:18
  • You could probably use the json_encode function that's available in PHP (if you have the JSON extension enabled): php.net/manual/en/function.json-encode.php Commented Jul 5, 2012 at 12:56

2 Answers 2

1

The $.inArray(course_id, subjects.id) part won't work, as id is a key of an object inside the subjects array (thus not a property of subjects). Try like that:

function inArrayById(array, value)
    $.each(array, function(i, element) {
        if(element.id === value){
            return true;
        }
    });
    return false;
}

alert( inArrayById(subjects, course_id) )
Sign up to request clarification or add additional context in comments.

7 Comments

Okay, I'll give it a whirl. Do I have to use an each loop though?
Haha nothing, I just wouldn't mind being able to do it with a variable for re-use. I guess I can just re-used the function you just wrote up?
I gave it a go. It seems to only return false.
Did you pass course_id as number and not string? Are you sure the object you're looking for is in the array? Because Secator's code is correct, and it's a very basic loop.
Well instead of using that variable, I just passed in a number directly that I know is in the array, and, still false.
|
0

This is a variant of Secators function, except it works without jQuery.

function inArrayById(array, value) {
    for(var i = 0; i < array.length; i++) {
        if(array[i]["id"] === value) return true;
    }

    return false;
}

1 Comment

This seemed to work right off the bat for what I need. Thanks mate!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.